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.

 1 <?php
 2 
 3 use Drupal\block\Entity\Block;
 4 use Drupal\block_content\Entity\BlockContent;
 5 
 6 /**
 7  * Implements hook_install().
 8  */
 9 function awesome_blocks_install() {
10   $block_content = BlockContent::create([
11     'type' => 'basic',
12     'info' => 'About us',
13   ]);
14   $block_content->set('body', 'Some long text about us...');
15   $block_content->save();
16 
17   $block = Block::create([
18     'id' => 'about_us',
19     'plugin' => 'block_content:' . $block_content->uuid(),
20     'region' => 'header',
21     'provider' => 'block_content',
22     'weight' => -100,
23     'theme' => \Drupal::config('system.theme')->get('default'),
24     'visibility' => array(),
25     'settings' => [
26       'label' => 'About us',
27       'label_display' => FALSE,
28     ],
29   ]);
30   $block->save();
31 }

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