5 * Contains install and update functions for Block.
8 use Drupal\Core\Cache\Cache;
11 * Implements hook_install().
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']);
21 * Update block visibility context mapping.
23 function block_update_8001() {
24 // This update function updates blocks for the change from
25 // https://www.drupal.org/node/2354889.
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.
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',
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;
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 = [];
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
60 $backup_values[$context][] = $condition_plugin_id;
61 unset($visibility[$condition_plugin_id]);
64 // Replace the context ID based on the defined mapping.
65 $condition['context_mapping'][$key] = $context_service_id_map[$context];
68 $block->set('visibility', $visibility);
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
74 $update_backup[$block->get('id')] = [
75 'missing_context_ids' => $backup_values,
76 'status' => $block->get('status')
81 // Mark the resulting configuration as trusted data. This avoids issues with
82 // future schema changes.
87 \Drupal::keyValue('update_backup')->set('block_update_8001', $update_backup);
90 return t('Block context IDs updated.');
94 * Placeholder for the previous 8002 update.
96 function block_update_8002() {
97 \Drupal::state()->set('block_update_8002_placeholder', TRUE);
101 * Remove 'cache' setting.
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);
108 // Remove the 'cache' setting.
109 $settings = $block->get('settings');
110 unset($settings['cache']);
111 $block->set('settings', $settings);
113 // Mark the resulting configuration as trusted data. This avoids issues with
114 // future schema changes.
118 return t('Block settings updated.');