Creating blocks using plugins
In Drupal, a block is a piece of content that can be placed in a region provided by a theme. Blocks are used to present specific kinds of content, such as a user login form, a snippet of text, and many more.
Blocks are annotated plugins. Annotated plugins use documentation blocks to provide details of the plugin. They are discovered in the module’s Plugin
class namespace. Each class in the Plugin/Block
namespace will be discovered by the Block
module’s plugin manager.
In this recipe, we will define a block that will display a copyright snippet and the current year and place it in the footer region.
How to do it…
- First, we need to create the
src/Plugin/Block
directory in the module’s directory. This will translate the\Drupal\mymodule\Plugin\Block
namespace and allow block plugin discovery:mkdir -p src/Plugin/Block
- Create a file named
Copyright.php
in the newly created directory so that we can define the...