1. 8.5.x core/modules/block/block.api.php block_api
  2. 8.0.x core/modules/block/block.api.php block_api
  3. 8.1.x core/modules/block/block.api.php block_api
  4. 8.2.x core/modules/block/block.api.php block_api
  5. 8.3.x core/modules/block/block.api.php block_api
  6. 8.4.x core/modules/block/block.api.php block_api
  7. 8.6.x core/modules/block/block.api.php 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:


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


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


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


Namesort descending Location Description
BlockPluginInterface core/lib/Drupal/Core/Block/BlockPluginInterface.php Defines the required interface for all block plugins.
MainContentBlockPluginInterface core/lib/Drupal/Core/Block/MainContentBlockPluginInterface.php The interface for "main page content" blocks.
MessagesBlockPluginInterface core/lib/Drupal/Core/Block/MessagesBlockPluginInterface.php The interface for "messages" (#type => status_messages) blocks.
TitleBlockPluginInterface core/lib/Drupal/Core/Block/TitleBlockPluginInterface.php The interface for "title" blocks.


MKorostoff’s picture

Here's an example of how to create your first Block. Create a file at the location modules/yourmodule/src/Plugin/Block/YourBlockName.php. Contents should be as follows:

 * @file
 * Contains \Drupal\yourmodule\Plugin\Block\YourBlockName.

namespace Drupal\yourmodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;

 * Provides my custom block.
 * @Block(
 *   id = "my_custom_block",
 *   admin_label = @Translation("My Custom Block"),
 *   category = @Translation("Blocks")
 * )
class YourBlockName extends BlockBase {

   * {@inheritdoc}
  public function build() {
    return array('#markup' => 'hello world');


Note that the comment/docblock in required before your class definition.