You are here

function hook_block_view_alter in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/block/block.api.php \hook_block_view_alter()
  2. 7 modules/block/block.api.php \hook_block_view_alter()
  3. 9 core/modules/block/block.api.php \hook_block_view_alter()

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\Core\Render\RendererInterface::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().
1 invocation of hook_block_view_alter()
BlockViewBuilder::buildPreRenderableBlock in core/modules/block/src/BlockViewBuilder.php
Builds a #pre_render-able block render array.

File

core/modules/block/block.api.php, line 95
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']);
  }
}