Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / block / tests / modules / block_test / block_test.module
1 <?php
2
3 /**
4  * @file
5  * Provide test blocks.
6  */
7
8 use Drupal\Core\Block\BlockPluginInterface;
9 use Drupal\Core\Cache\Cache;
10
11 /**
12  * Implements hook_block_alter().
13  */
14 function block_test_block_alter(&$block_info) {
15   if (\Drupal::state()->get('block_test_info_alter') && isset($block_info['test_block_instantiation'])) {
16     $block_info['test_block_instantiation']['category'] = t('Custom category');
17   }
18 }
19
20 /**
21  * Implements hook_block_view_BASE_BLOCK_ID_alter().
22  */
23 function block_test_block_view_test_cache_alter(array &$build, BlockPluginInterface $block) {
24   if (\Drupal::state()->get('block_test_view_alter_suffix') !== NULL) {
25     $build['#attributes']['foo'] = 'bar';
26   }
27   if (\Drupal::state()->get('block_test_view_alter_append_pre_render_prefix') !== NULL) {
28     $build['#pre_render'][] = 'block_test_pre_render_alter_content';
29   }
30 }
31
32 /**
33  * Implements hook_block_build_BASE_BLOCK_ID_alter().
34  */
35 function block_test_block_build_test_cache_alter(array &$build, BlockPluginInterface $block) {
36   // Test altering cache keys, contexts, tags and max-age.
37   if (\Drupal::state()->get('block_test_block_alter_cache_key') !== NULL) {
38     $build['#cache']['keys'][] = \Drupal::state()->get('block_test_block_alter_cache_key');
39   }
40   if (\Drupal::state()->get('block_test_block_alter_cache_context') !== NULL) {
41     $build['#cache']['contexts'][] = \Drupal::state()->get('block_test_block_alter_cache_context');
42   }
43   if (\Drupal::state()->get('block_test_block_alter_cache_tag') !== NULL) {
44     $build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], [\Drupal::state()->get('block_test_block_alter_cache_tag')]);
45   }
46   if (\Drupal::state()->get('block_test_block_alter_cache_max_age') !== NULL) {
47     $build['#cache']['max-age'] = \Drupal::state()->get('block_test_block_alter_cache_max_age');
48   }
49
50   // Test setting #create_placeholder.
51   if (\Drupal::state()->get('block_test_block_alter_create_placeholder') !== NULL) {
52     $build['#create_placeholder'] = \Drupal::state()->get('block_test_block_alter_create_placeholder');
53   }
54 }
55
56 /**
57  * #pre_render callback for a block to alter its content.
58  */
59 function block_test_pre_render_alter_content($build) {
60   $build['#prefix'] = 'Hiya!<br>';
61   return $build;
62 }