function blocache_block_build_alter in Blocache (Block Cache Control) 8
Implements hook_block_build_alter().
File
- ./
blocache.module, line 34 - Provides a structure for block cache control.
Code
function blocache_block_build_alter(array &$build, BlockPluginInterface $block) {
$block_id = $build['#cache']['keys'][2];
$block_entity = Drupal::entityTypeManager()
->getStorage('block')
->load($block_id);
if (!$block_entity instanceof BlockInterface) {
return;
}
$blocache_metadata = Drupal::service('blocache.metadata');
$blocache_metadata
->setBlock($block_entity);
if ($blocache_metadata
->isOverridden()) {
$metadata = $blocache_metadata
->getMetadata();
$build['#cache']['max-age'] = $metadata['max-age'];
$build['#cache']['contexts'] = $metadata['contexts'];
$build['#cache']['tags'] = $metadata['tags'];
// This disable page caching wherever this block has been placed.
if ($metadata['max-age'] === 0) {
\Drupal::service('page_cache_kill_switch')
->trigger();
}
}
}