Skip to content

Block

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

Overview

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.

Basic requirements

To define a block in a module you need to:

This is an example of a basic block plugin class:

namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;

class MyBlock extends BlockBase {
  public function build() {
    return [
      '#type' => '#markup',
      '#markup' => 'Example block',
    ];
  }

}

More examples are available at the links below.

Extending blocks with conditions and hooks

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