6545a1997a590fea155dc6b36c6c5a21654a637f
[yaffs-website] / web / core / modules / block / block.install
1 <?php
2
3 /**
4  * @file
5  * Contains install and update functions for Block.
6  */
7
8 use Drupal\Core\Cache\Cache;
9
10 /**
11  * Implements hook_install().
12  */
13 function block_install() {
14   // Because the Block module upon installation unconditionally overrides all
15   // HTML output by selecting a different page display variant, we must
16   // invalidate all cached HTML output.
17   Cache::invalidateTags(['rendered']);
18 }
19
20 /**
21  * Update block visibility context mapping.
22  */
23 function block_update_8001() {
24   // This update function updates blocks for the change from
25   // https://www.drupal.org/node/2354889.
26
27   // Core visibility context plugins are updated automatically; blocks with
28   // unknown plugins are disabled and their previous visibility settings are
29   // saved in key value storage; see change record
30   // https://www.drupal.org/node/2527840 for more explanation.
31
32   // These are all the contexts that Drupal core provides.
33   $context_service_id_map = [
34     'node.node' => '@node.node_route_context:node',
35     'user.current_user' => '@user.current_user_context:current_user',
36   ];
37
38   foreach (array_keys(\Drupal::languageManager()->getDefinedLanguageTypesInfo()) as $language_type_id) {
39     $context_service_id_map['language.' . $language_type_id] = '@language.current_language_context:' . $language_type_id;
40   }
41
42   // Contributed modules should leverage hook_update_dependencies() in order to
43   // be executed after block_update_8001(). The blocks are then disabled if the
44   // contexts are still missing via
45   // block_post_update_disable_blocks_with_missing_contexts().
46   $config_factory = \Drupal::configFactory();
47   $backup_values = $update_backup = [];
48
49   foreach ($config_factory->listAll('block.block.') as $block_config_name) {
50     $block = $config_factory->getEditable($block_config_name);
51     if ($visibility = $block->get('visibility')) {
52       foreach ($visibility as $condition_plugin_id => &$condition) {
53         foreach ($condition['context_mapping'] as $key => $context) {
54           if (!isset($context_service_id_map[$context])) {
55             // Remove the visibility condition for unknown context mapping
56             // entries, so the update process itself runs through and users can
57             // fix their block placements manually OR alternatively contributed
58             // modules can run their own update functions to update mappings
59             // that they provide.
60             $backup_values[$context][] = $condition_plugin_id;
61             unset($visibility[$condition_plugin_id]);
62             continue;
63           }
64           // Replace the context ID based on the defined mapping.
65           $condition['context_mapping'][$key] = $context_service_id_map[$context];
66         }
67       }
68       $block->set('visibility', $visibility);
69
70       if ($backup_values) {
71         // We not only store the missing context mappings but also the previous
72         // block status, in order to allow contributed and custom modules to do
73         // their own updates.
74         $update_backup[$block->get('id')] = [
75           'missing_context_ids' => $backup_values,
76           'status' => $block->get('status')
77         ];
78       }
79     }
80
81     // Mark the resulting configuration as trusted data. This avoids issues with
82     // future schema changes.
83     $block->save(TRUE);
84   }
85
86   if ($update_backup) {
87     \Drupal::keyValue('update_backup')->set('block_update_8001', $update_backup);
88   }
89
90   return t('Block context IDs updated.');
91 }
92
93 /**
94  * Placeholder for the previous 8002 update.
95  */
96 function block_update_8002() {
97   \Drupal::state()->set('block_update_8002_placeholder', TRUE);
98 }
99
100 /**
101  * Remove 'cache' setting.
102  */
103 function block_update_8003() {
104   $config_factory = \Drupal::configFactory();
105   foreach ($config_factory->listAll('block.block.') as $block_config_name) {
106     $block = $config_factory->getEditable($block_config_name);
107
108     // Remove the 'cache' setting.
109     $settings = $block->get('settings');
110     unset($settings['cache']);
111     $block->set('settings', $settings);
112
113     // Mark the resulting configuration as trusted data. This avoids issues with
114     // future schema changes.
115     $block->save(TRUE);
116   }
117
118   return t('Block settings updated.');
119 }