Block API

Information about the classes and interfaces that make up the Block API.

Blocks are a combination of a configuration entity and a plugin. The configuration entity stores placement information (theme, region, weight) and any other configuration that is specific to the block. The block plugin does the work of rendering the block's content for display.

To define a block in a module you need to:

The Block API also makes use of Condition plugins, for conditional block placement. Condition plugins have interface \Drupal\Core\Condition\ConditionInterface, base class \Drupal\Core\Condition\ConditionPluginBase, and go in plugin namespace Plugin\Condition. Again, see the Plugin API and Annotations topics for details of how to create a plugin class and annotate it.

There are also several block-related hooks, which allow you to affect the content and access permissions for blocks:

Further information and examples:

Functions

Namesort descending Location Description
hook_block_access drupal/core/modules/block/block.api.php Control access to a block instance.
hook_block_view_alter drupal/core/modules/block/block.api.php Alter the result of \Drupal\block\BlockBase::build().
hook_block_view_BASE_BLOCK_ID_alter drupal/core/modules/block/block.api.php Provide a block plugin specific block_view alteration.

Classes

Namesort descending Location Description
Block drupal/core/modules/block/src/Annotation/Block.php Defines a Block annotation object.
BlockBase drupal/core/modules/block/src/BlockBase.php Defines a base block implementation that most blocks plugins will extend.

Interfaces

Namesort descending Location Description
BlockPluginInterface drupal/core/modules/block/src/BlockPluginInterface.php Defines the required interface for all block plugins.

File

drupal/core/modules/block/block.api.php, line 8
Hooks provided by the Block module.