Drupal 8  8.0.2
Block API

Data Structures

class  Block
 
class  BlockBase
 
interface  BlockPluginInterface
 
interface  MainContentBlockPluginInterface
 
interface  MessagesBlockPluginInterface
 
interface  TitleBlockPluginInterface
 

Functions

 hook_block_view_alter (array &$build,\Drupal\Core\Block\BlockPluginInterface $block)
 
 hook_block_view_BASE_BLOCK_ID_alter (array &$build,\Drupal\Core\Block\BlockPluginInterface $block)
 
 hook_block_build_alter (array &$build,\Drupal\Core\Block\BlockPluginInterface $block)
 
 hook_block_build_BASE_BLOCK_ID_alter (array &$build,\Drupal\Core\Block\BlockPluginInterface $block)
 
 hook_block_access (\Drupal\block\Entity\Block $block, $operation,\Drupal\Core\Session\AccountInterface $account)
 

Detailed Description

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 , base class , and go in plugin namespace Plugin. 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:

Function Documentation

hook_block_access ( \Drupal\block\Entity\Block  $block,
  $operation,
\Drupal\Core\Session\AccountInterface  $account 
)

Control access to a block instance.

Modules may implement this hook if they want to have a say in whether or not a given user has access to perform a given operation on a block instance.

Parameters
\Drupal\block\Entity\Block$blockThe block instance.
string$operationThe operation to be performed; for instance, 'view', 'create', 'delete', or 'update'.
\Drupal\Core\Session\AccountInterface$accountThe user object to perform the access check operation on.
Returns
The access result. If all implementations of this hook return AccessResultInterface objects whose value is !isAllowed() and !isForbidden(), then default access rules from ::checkAccess() are used.
See Also
::access()
::checkAccess()
hook_block_build_alter ( array &  $build,
\Drupal\Core\Block\BlockPluginInterface  $block 
)

Alter the result of ::build().

Unlike hook_block_view_alter(), this hook is called very early, before the block is being assembled. Therefore, it is early enough to alter the cacheability metadata (change #cache), or to explicitly placeholder the block (set #create_placeholder).

In addition to hook_block_build_alter(), which is called for all blocks, there is hook_block_build_BASE_BLOCK_ID_alter(), which can be used to target a specific block or set of similar blocks.

Parameters
array&$buildA renderable array of data, only containing #cache.
\Drupal\Core\Block\BlockPluginInterface$blockThe block plugin instance.
See Also
hook_block_build_BASE_BLOCK_ID_alter()
Entity CRUD, editing, and view hooks
hook_block_build_BASE_BLOCK_ID_alter ( array &  $build,
\Drupal\Core\Block\BlockPluginInterface  $block 
)

Provide a block plugin specific block_build alteration.

In this hook name, BASE_BLOCK_ID refers to the block implementation's plugin id, regardless of whether the plugin supports derivatives. For example, for the block, this would be 'system_powered_by_block' as per that class's annotation. And for the block, it would be 'system_menu_block' as per that class's annotation, regardless of which menu the derived block is for.

Parameters
array$buildA renderable array of data, only containing #cache.
\Drupal\Core\Block\BlockPluginInterface$blockThe block plugin instance.
See Also
hook_block_build_alter()
Entity CRUD, editing, and view hooks
hook_block_view_alter ( array &  $build,
\Drupal\Core\Block\BlockPluginInterface  $block 
)

Alter the result of ::build().

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete block content structure has been built.

If the module wishes to act on the rendered HTML of the block rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_HOOK() for block.html.twig. See drupal_render() documentation or the Default theme implementations topic for details.

In addition to hook_block_view_alter(), which is called for all blocks, there is hook_block_view_BASE_BLOCK_ID_alter(), which can be used to target a specific block or set of similar blocks.

Parameters
array&$buildA renderable array of data, as returned from the build() implementation of the plugin that defined the block:
  • #title: The default localized title of the block.
\Drupal\Core\Block\BlockPluginInterface$blockThe block plugin instance.
See Also
hook_block_view_BASE_BLOCK_ID_alter()
Entity CRUD, editing, and view hooks
hook_block_view_BASE_BLOCK_ID_alter ( array &  $build,
\Drupal\Core\Block\BlockPluginInterface  $block 
)

Provide a block plugin specific block_view alteration.

In this hook name, BASE_BLOCK_ID refers to the block implementation's plugin id, regardless of whether the plugin supports derivatives. For example, for the block, this would be 'system_powered_by_block' as per that class's annotation. And for the block, it would be 'system_menu_block' as per that class's annotation, regardless of which menu the derived block is for.

Parameters
array$buildA renderable array of data, as returned from the build() implementation of the plugin that defined the block:
  • #title: The default localized title of the block.
\Drupal\Core\Block\BlockPluginInterface$blockThe block plugin instance.
See Also
hook_block_view_alter()
Entity CRUD, editing, and view hooks

References t().

Here is the call graph for this function: