Drupal 8
Blog

Drupal 8: Creating custom contact us block with form field

Before we jump into the custom block writing. I would like to go through the blocker, which came to my learning. Hope it would be clear and simplest way to explain.

Annotations based plugin discovery
Annotation is a meta data for our block. similar to hook_block_info did in drupal 7.

which carries machine readable name, admin label, category etc. drupal has Annotations-based plugin discovery mechanism which read the meta data and let the module knows that particular plugin is an implementation of block. Because of that it implement block plugin interface. it provides various method like build and block form, which drupal knows it exist & called up whenever  we require displaying content in our block.

Here's what the Drupal handbook has to say about plugins:

Most of the plugins in Drupal 8 will use annotations to register themselves and describe their metadata. Some of the plugins types provided by core are:

  1. Blocks (see */src/Plugin/Block/* for many examples)

  2. Field formatters, Field widgets (see */src/Plugin/Field/* for many examples)

  3. All Views plugins (see */src/Plugin/views/* for many examples)

   
Conditions( used for Block visibility in core)

s10
  • The 'id' property in annotation defines unique & machine readable ID for block, and the name of the block to be seen by other code.

  • The 'admin_label' annotation defines the human readable name of the block that will be used when displaying your block in the administration UI.

    s6
     

  • The ‘category’ defines the package for your custom block.

    s7



Plugin API

In Drupal 8 Plugin allow our module to provide additional functionality by reusing the code & implement the interface with different functionality. In Drupal every block consists of the same parts, label, content, and various settings related to visibility and cachability. How that label and content is generated is different varies from one module to another.

The plugin system has three base elements:

Plugin Types: Is the controlling class that defines how the plugins of this type will be discovered and instantiated. The type will describe the central purpose of all plugins of that type; e.g. blocks, etc.

Plugin Discovery: is the process of finding plugins within the available code base that qualify for use within this particular plugin type's use case.

Plugin Factory: The Factory is responsible for instantiating the specific plugin(s) chosen for a given use case.

In this Custom block i’m going to add some fields(org name/ org location/ email id/ phone/ address). i’m saving theses values to my block configuration page. and also validating one of the field (org name) should not be numeric.

Defines a base block implementation that most blocks plugins will extend.

This class provides the generic block configuration form, default block settings, and handling for general user-defined block visibility settings.

$form = parent::blockForm($form, $form_state);

It creates a form array and get all the data from the parent method.

s12

saves the block configuration data.

s13

Process the block's submission handling if no errors occurred.

s14

Get the block configuration.

s15

To add validation for a specific block type, override BlockBase::blockValidate().

Once we have done with block build, block form, save configuration, get configuration, validate and submit. we will find our custom block to be listed in our block listing page.

Below is the Block form layout of our custom Block module


s16

After filling up all the detail save the block. and our custom block start displaying in the specified region.

s17

Git Source code: https://github.com/xaiwant/D8custom-block-with-field-data

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch