Creating custom blocks

Video Link: Custom Block Creation, Add access check, Create Block form, Dependency injection.

In this guide you will learn how to programmatically add a block to the block layout interface. How to add a (admin) configuration form to the block, and how to process the form. Finally, you will learn how to add and display default configuration values for the form.

Before starting this walk-through, please prepare the module skeleton as per the instructions Prepare a Module skeleton.

Create a custom block plugin

Blocks in Drupal are instances of the block plugin.

Add a Form to the Block Configuration

Now let us say that we want to add the ability for the site builder to enter a piece of configuration for each instance of our custom block.

Process the Block Config Form

Add the following method to the HelloBlock class. In this example, it is located in the src/Plugin/Block/HelloBlock.php file, but as you

Use Config in Block Display

To make use of the configuration of instances of the block, we can modify the build() method of the HelloBlock class:

Guide maintainers

pancho's picture