8.5.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
8.0.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
8.1.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
8.2.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
8.3.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
8.4.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
8.6.x block.api.php hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
7.x block.api.php hook_block_view_alter(&$data, $block)

Alter the result of \Drupal\Core\Block\BlockBase::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 &$build: A 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 $block: The block plugin instance.

See also

hook_block_view_BASE_BLOCK_ID_alter()

Entity CRUD, editing, and view hooks

Related topics

2 functions implement hook_block_view_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

contextual_test_block_view_alter in core/modules/contextual/tests/modules/contextual_test/contextual_test.module
Implements hook_block_view_alter().
settings_tray_block_view_alter in core/modules/settings_tray/settings_tray.module
Implements hook_block_view_alter().

File

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

Code

function hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {

  // Remove the contextual links on all blocks that provide them.
  if (isset($build['#contextual_links'])) {
    unset($build['#contextual_links']);
  }
}

Comments

mstef’s picture

Seems the only way to actually access the result of BlockBase::build() is to add a #pre_render in this hook, then use your callback to alter $build. Not sure why that is, but at least the documentation should be updated.