function block_test_block_build_test_cache_alter in Drupal 9
Same name and namespace in other branches
- 8 core/modules/block/tests/modules/block_test/block_test.module \block_test_block_build_test_cache_alter()
Implements hook_block_build_BASE_BLOCK_ID_alter().
File
- core/
modules/ block/ tests/ modules/ block_test/ block_test.module, line 35 - Provide test blocks.
Code
function block_test_block_build_test_cache_alter(array &$build, BlockPluginInterface $block) {
// Test altering cache keys, contexts, tags and max-age.
if (\Drupal::state()
->get('block_test_block_alter_cache_key') !== NULL) {
$build['#cache']['keys'][] = \Drupal::state()
->get('block_test_block_alter_cache_key');
}
if (\Drupal::state()
->get('block_test_block_alter_cache_context') !== NULL) {
$build['#cache']['contexts'][] = \Drupal::state()
->get('block_test_block_alter_cache_context');
}
if (\Drupal::state()
->get('block_test_block_alter_cache_tag') !== NULL) {
$build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], [
\Drupal::state()
->get('block_test_block_alter_cache_tag'),
]);
}
if (\Drupal::state()
->get('block_test_block_alter_cache_max_age') !== NULL) {
$build['#cache']['max-age'] = \Drupal::state()
->get('block_test_block_alter_cache_max_age');
}
// Test setting #create_placeholder.
if (\Drupal::state()
->get('block_test_block_alter_create_placeholder') !== NULL) {
$build['#create_placeholder'] = \Drupal::state()
->get('block_test_block_alter_create_placeholder');
}
}