How to create a custom block and assigning to a region in Drupal 8
One of the many changes in Drupal 8 is adding a block to a region. The block interface has been pretty consistent over the years, so changes to how it works can be confusing at first. You do something over and over again and then “Wait a minute! Things have moved, what do I do?!”. But never fear, the new way of adding blocks to regions is pretty straight forward once you get your head around it.
Create a new block
For this demonstration, we are going to create a new block in the blocks interface (you could also create a block in a custom module, or it could come from another module like Views).
In the admin menu, go to Structure and then Block layout.
You will then land on the main blocks interface page and see a list of all existing blocks and the regions that they belong to.
At the top of the page, you will see two tabs: Block layout and Custom block library. You are currently on the Block layout tab. Click on the Custom block library tab.
You are going to add your own module, so click on the + Add custom block button at the top of the modal. You will then get the add block form. Add the following for the description and body:
- Description: Hello block
- Body: Hello, World!
Save the form. You should then get a message to say that the block has been created.
The block you created should now appear on the list of custom blocks.
Next to Sidebar first you will see a Place block button.
You can add the new block to any region. For the purposes of this exercise, let’s add the block to the Sidebar first region.
Add the block to a region, head back over to the Block layout tab. To add the block to the sidebar first region, click on the Place block.
A modal will appear with a list of available blocks. These blocks have already been defined in various modules or are custom modules (like the one you just created). You should see the Hello block block that you created above. If it is hard to spot in the list, you can search for it in the Filter by block name field at the top of the modal. To add the block to this region, click on the Place block. Then on the next form (Configure block), click Save
You should then see the block listed under Sidebar first.
View the block
Go to the homepage of the site. You should now see the block you created in the sidebar left of your site.
Assigning the block to another region
In another change in Drupal 8, you can now assign a block to more than one region. To demonstrate this, try adding the block you created to the Sidebar second region.
- Go back to the main blocks interface
- Next to sidebar second click on Place a block
- In the filter by block name field, search for hello. The hello block you created above should be visible
- In the operations column, click on place block.
There are many changes in Drupal 8. I’m going to sharing tutorials and guides to many of them over the coming weeks and months. Be the first to get them
Drupal 8: Controllers, Routes, Form classes, the death of hooks? Oh my, so many changes! What do I start?!
Learn the first steps in starting your new Drupal module in just 20 minutes per day. You’ll get your feet wet in the new world of Drupal 8 module development and get over the hardest hurdle of all - getting started without being overwhelmed.