This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to Create a Custom Block in Drupal 8

Updated on  26th February

4 Min Read
Reading Time: 4 minutes

Drupal is popular mainly because of its flexibility. Almost anything in the robust CMS can be modified or customized to fit the needs of a website developer.

create custom block drupal 8

One of the main aspects of Drupal is the way it lays out a site page through ‘blocks’. Although there are some pretty useful and out of the box blocks available in Drupal, there are times when you need to create a custom block and Drupal makes it pretty easy to create one. However, for newbies navigating through the menus might be complex.

See: Top 10 Things Every Drupal Developer Should Know

In this tutorial, I’ll show you how to create a custom block in Drupal.

Login to the Admin Panel

The first thing that you need to do is log in to Drupal as an admin. Now navigate to admin -> structure -> block layout -> custom block library.

Drupal block layout page

Custom Block Type

Once here, click the ‘Block types’ tab. Then click the blue ‘Add custom block type’ button.

Drupal custom block library

Now, enter the Label and Description of your custom block type, and click Save.

Custom block details

Edit Fields

You can now edit or add fields to your custom block type as required. Not only that, you also have the option to Manage display etc. Customize your block as much as you need. Here, I’ll be adding a file field. This will allow a user to download a particular file.

Manage Fields

Type of input

Add Custom Block

Now go back to the ‘Custom block library’ tab under Block Layout and click ‘Add custom block’ to add you block to the library.

Custom Block library Drupal

Here, you’ll see the block we created in the previous steps.

Add Custom Block Drupal

Now enter the block description, body and the file you wish for the users to download. Click Save and your block should appear in the list under ‘Custom Block Library’.

Place Block

All that’s left for you to do now is to place the block on the site. You can place it wherever you wish. I’ll be placing it in the 1st sidebar just as an example. You can do this by navigating to admin -> structure -> block layout.

Click the ‘Place Block’ button beside the region you want to place your block in. Since I’ll be placing it in the left sidebar, I can find the region and click ‘Place block.’ Here a list of blocks that will appear. Find your custom block and click ‘Place block’.

Place Custom Block Drupal
You will be presented with an option to set the visibility of the block. This way it’ll be visible for certain content types only. After making the appropriate changes, click Save. Now, go back to your site and look for the region you placed your block in.

Custom block on Drupal website

Conclusion

That’s it! You’ve successfully created your own custom block and placed it in your site. This was a very basic example of the custom block functionality. You can make blocks for certain content types, place it in any region, and create any type of block. There are a lot of possibilities and the customization is seemingly endless. Hope you found this tutorial useful.

If you have any questions, please leave me a comment and I’ll get back to you.

Share your opinion in the comment section. COMMENT NOW

Hamza Zia

Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.