From 51c1abcdbdab4224eadeb5d1d6d917ac662716cf Mon Sep 17 00:00:00 2001 From: Miroslav Michalicka Date: Thu, 17 Dec 2015 10:14:58 +0100 Subject: [PATCH] New blog post about custom blocks. --- 2015/12/07/views-twig/index.html | 4 + 2015/12/17/custom-blocks/index.html | 230 ++++++++++++++++++++++++++++ feed.xml | 71 ++++++++- index.html | 14 ++ 4 files changed, 317 insertions(+), 2 deletions(-) create mode 100644 2015/12/17/custom-blocks/index.html diff --git a/2015/12/07/views-twig/index.html b/2015/12/07/views-twig/index.html index b2dca2e..f9eb771 100644 --- a/2015/12/07/views-twig/index.html +++ b/2015/12/07/views-twig/index.html @@ -131,6 +131,10 @@

Conclusion

+ + diff --git a/2015/12/17/custom-blocks/index.html b/2015/12/17/custom-blocks/index.html new file mode 100644 index 0000000..b0f1311 --- /dev/null +++ b/2015/12/17/custom-blocks/index.html @@ -0,0 +1,230 @@ + + + + + + + + + + + Custom blocks in Drupal 8 - Miro Michalicka + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+

Custom blocks in Drupal 8

+ +

CMI won't stop you

+ + Posted on December 17, 2015 +
+
+
+
+
+ + +
+
+
+
+ +

You have probably notices that CMI has some limitation in exporting custom blocks. These are unfortunately quite important for Front enders and also for client. Let me show you, how you can simply create custom blocks in code during installation of your module.

+ +

If you are reading this blog, I assume you know that blocks are finally entities in Drupal 8. You can define your own block type and add some fields into it. For simple demonstration I’m using predefined custom block type Basic block with body text field. Same approach can be of course used for your custom block types with many additional fiels.

+ +

First you need to create Custom block:

+ +
 1 <?php
+ 2 
+ 3 use Drupal\block_content\Entity\BlockContent;
+ 4 
+ 5 /**
+ 6  * Implements hook_install().
+ 7  */
+ 8 function awesome_blocks_install() {
+ 9   $block_content = BlockContent::create([
+10     'type' => 'basic',
+11     'info' => 'About us',
+12   ]);
+13   $block_content->set('body', 'Some long text about us...');
+14   $block_content->save();
+15 }
+ +

If you install your module now, you can find this block in Custom block library. You can update all fields which is big change since Drupal 7, but you still need to show your block somewhere on page. Because block are entities, they can be displayed multiple times. You need to create block instance which can be placed into region and visibility settings can be applied.

+ +
<?php
+
+use Drupal\block\Entity\Block;
+use Drupal\block_content\Entity\BlockContent;
+
+/**
+ * Implements hook_install().
+ */
+function awesome_blocks_install() {
+  $block_content = BlockContent::create([
+    'type' => 'basic',
+    'info' => 'About us',
+  ]);
+  $block_content->set('body', 'Some long text about us...');
+  $block_content->save();
+
+  $block = Block::create([
+    'id' => 'about_us',
+    'plugin' => 'block_content:' . $block_content->uuid(),
+    'region' => 'header',
+    'provider' => 'block_content',
+    'weight' => -100,
+    'theme' => \Drupal::config('system.theme')->get('default'),
+    'visibility' => array(),
+    'settings' => [
+      'label' => 'About us',
+      'label_display' => FALSE,
+    ],
+  ]);
+  $block->save();
+}
+ +

Now you just need to put this code into your module’s .install file and enable it. Enjoy :)

+ + +
+ + + +
+
+
+
+ +
+ + + + + + + + + + + + + + + + + + diff --git a/feed.xml b/feed.xml index 58dce76..24fab28 100755 --- a/feed.xml +++ b/feed.xml @@ -5,10 +5,77 @@ Drupal and travelling http://yourdomain.com/ - Tue, 08 Dec 2015 16:14:13 +0100 - Tue, 08 Dec 2015 16:14:13 +0100 + Thu, 17 Dec 2015 10:13:44 +0100 + Thu, 17 Dec 2015 10:13:44 +0100 Jekyll v3.0.0 + + Custom blocks in Drupal 8 + <p>You have probably notices that CMI has some limitation in exporting custom blocks. These are unfortunately quite important for Front enders and also for client. Let me show you, how you can simply create custom blocks in code during installation of your module.</p> + +<p>If you are reading this blog, I assume you know that blocks are finally entities in Drupal 8. You can define your own block type and add some fields into it. For simple demonstration I’m using predefined custom block type Basic block with body text field. Same approach can be of course used for your custom block types with many additional fiels.</p> + +<p>First you need to create Custom block:</p> + +<figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="lineno"> 1</span> <span class="cp">&lt;?php</span> +<span class="lineno"> 2</span> +<span class="lineno"> 3</span> <span class="k">use</span> <span class="nx">Drupal\block_content\Entity\BlockContent</span><span class="p">;</span> +<span class="lineno"> 4</span> +<span class="lineno"> 5</span> <span class="sd">/**</span> +<span class="lineno"> 6</span> <span class="sd"> * Implements hook_install().</span> +<span class="lineno"> 7</span> <span class="sd"> */</span> +<span class="lineno"> 8</span> <span class="k">function</span> <span class="nf">awesome_blocks_install</span><span class="p">()</span> <span class="p">{</span> +<span class="lineno"> 9</span> <span class="nv">$block_content</span> <span class="o">=</span> <span class="nx">BlockContent</span><span class="o">::</span><span class="na">create</span><span class="p">([</span> +<span class="lineno">10</span> <span class="s1">&#39;type&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;basic&#39;</span><span class="p">,</span> +<span class="lineno">11</span> <span class="s1">&#39;info&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;About us&#39;</span><span class="p">,</span> +<span class="lineno">12</span> <span class="p">]);</span> +<span class="lineno">13</span> <span class="nv">$block_content</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="s1">&#39;Some long text about us...&#39;</span><span class="p">);</span> +<span class="lineno">14</span> <span class="nv">$block_content</span><span class="o">-&gt;</span><span class="na">save</span><span class="p">();</span> +<span class="lineno">15</span> <span class="p">}</span></code></pre></figure> + +<p>If you install your module now, you can find this block in <a href="http://your-domain.tld/admin/structure/block/block-content/types">Custom block library</a>. You can update all fields which is big change since Drupal 7, but you still need to show your block somewhere on page. Because block are entities, they can be displayed multiple times. You need to create block instance which can be placed into region and visibility settings can be applied. </p> + +<figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> + +<span class="k">use</span> <span class="nx">Drupal\block\Entity\Block</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Drupal\block_content\Entity\BlockContent</span><span class="p">;</span> + +<span class="sd">/**</span> +<span class="sd"> * Implements hook_install().</span> +<span class="sd"> */</span> +<span class="k">function</span> <span class="nf">awesome_blocks_install</span><span class="p">()</span> <span class="p">{</span> + <span class="nv">$block_content</span> <span class="o">=</span> <span class="nx">BlockContent</span><span class="o">::</span><span class="na">create</span><span class="p">([</span> + <span class="s1">&#39;type&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;basic&#39;</span><span class="p">,</span> + <span class="s1">&#39;info&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;About us&#39;</span><span class="p">,</span> + <span class="p">]);</span> + <span class="nv">$block_content</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="s1">&#39;Some long text about us...&#39;</span><span class="p">);</span> + <span class="nv">$block_content</span><span class="o">-&gt;</span><span class="na">save</span><span class="p">();</span> + + <span class="nv">$block</span> <span class="o">=</span> <span class="nx">Block</span><span class="o">::</span><span class="na">create</span><span class="p">([</span> + <span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;about_us&#39;</span><span class="p">,</span> + <span class="s1">&#39;plugin&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;block_content:&#39;</span> <span class="o">.</span> <span class="nv">$block_content</span><span class="o">-&gt;</span><span class="na">uuid</span><span class="p">(),</span> + <span class="s1">&#39;region&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;header&#39;</span><span class="p">,</span> + <span class="s1">&#39;provider&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;block_content&#39;</span><span class="p">,</span> + <span class="s1">&#39;weight&#39;</span> <span class="o">=&gt;</span> <span class="o">-</span><span class="mi">100</span><span class="p">,</span> + <span class="s1">&#39;theme&#39;</span> <span class="o">=&gt;</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">config</span><span class="p">(</span><span class="s1">&#39;system.theme&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;default&#39;</span><span class="p">),</span> + <span class="s1">&#39;visibility&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(),</span> + <span class="s1">&#39;settings&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span> + <span class="s1">&#39;label&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;About us&#39;</span><span class="p">,</span> + <span class="s1">&#39;label_display&#39;</span> <span class="o">=&gt;</span> <span class="k">FALSE</span><span class="p">,</span> + <span class="p">],</span> + <span class="p">]);</span> + <span class="nv">$block</span><span class="o">-&gt;</span><span class="na">save</span><span class="p">();</span> +<span class="p">}</span></code></pre></figure> + +<p>Now you just need to put this code into your module’s .install file and enable it. Enjoy :)</p> + + Thu, 17 Dec 2015 08:00:00 +0100 + http://yourdomain.com/2015/12/17/custom-blocks/ + http://yourdomain.com/2015/12/17/custom-blocks/ + + + + Twig inside Views in Drupal 8 <p>Drupal 8 brings us except Symfony in backend also Twig for frontenders and Views for sitebuilders. Have you every thought about mixing this two parts of Drupal? In most of cases it is better if frontender knows at least basics of Drupal sitebuilding and Views are one of the most important modules for this.</p> diff --git a/index.html b/index.html index 3d1094d..73b7dd8 100755 --- a/index.html +++ b/index.html @@ -85,6 +85,20 @@

Miro Michalicka