W3cubDocs

/Drupal 8

function hook_block_view_alter

hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $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

Block API
Information about the classes and interfaces that make up the Block API.
Hooks
Define functions that alter the behavior of Drupal core.

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']);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!block!block.api.php/function/hook_block_view_alter/8.1.x