I need to alter all the blocks of custom type 'my_custom_block_type' that get displayed on my site. In order to achieve this, i added a custom prerender in which i filter on the block bundle :
function my_module_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
$build['#pre_render'][] = '_custom_block_custom_prerender';
}
function _custom_block_custom_prerender(array $build) {
if ($build['content']['#block_content']) {
$block_bundle = $build['content']['#block_content']->bundle();
if ($build['content']['#block_content']->bundle() === 'my_custom_block_type') {
// do your thing here
}
}
return $build;
}
I think it's not the most elegant way to do this.
I tried to enhance this, but i did not manage, after several attempts, to retrieve the block type directly inside a hook_block_view_alter or a hook_block_build_alter ...
function hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
// things
}
function hook_block_build_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
// stuff
}
Is there a way to easily get the custom block type inside those two hooks ?