Block
hook_block_build_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 documentation 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.
*
* @param 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.
* @param \Drupal\Core\Block\BlockPluginInterface $block
* The block plugin instance.
*
* @see hook_block_view_BASE_BLOCK_ID_alter()
* @see entity_crud
*/
function hook_block_view_alter(array &$build, BlockPluginInterface $block) {
// Remove the contextual links on all blocks that provide them.
if (isset($build['#contextual_links'])) {
unset($build['#contextual_links']);
}
}
hook_block_view_BASE_BLOCK_ID_alter
/**
* 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 \Drupal\system\Plugin\Block\SystemPoweredByBlock block, this would be
* 'system_powered_by_block' as per that class's annotation. And for the
* \Drupal\system\Plugin\Block\SystemMenuBlock block, it would be
* 'system_menu_block' as per that class's annotation, regardless of which menu
* the derived block is for.
*
* @param 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.
* @param \Drupal\Core\Block\BlockPluginInterface $block
* The block plugin instance.
*
* @see hook_block_view_alter()
* @see entity_crud
*/
function hook_block_view_BASE_BLOCK_ID_alter(array &$build, BlockPluginInterface $block) {
// Change the title of the specific block.
$build['#title'] = t('New title of the block');
}
hook_block_view_alter
/**
* Alter the result of \Drupal\Core\Block\BlockBase::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.
*
* @param array &$build
* A renderable array of data, only containing #cache.
* @param \Drupal\Core\Block\BlockPluginInterface $block
* The block plugin instance.
*
* @see hook_block_build_BASE_BLOCK_ID_alter()
* @see entity_crud
*/
function hook_block_build_alter(array &$build, BlockPluginInterface $block) {
// Add the 'user' cache context to some blocks.
if ($block->label() === 'some condition') {
$build['#cache']['contexts'][] = 'user';
}
}