Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Post update functions for Block.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Disable all blocks with missing context IDs in block_update_8001().
|
Chris@0
|
10 */
|
Chris@0
|
11 function block_post_update_disable_blocks_with_missing_contexts() {
|
Chris@0
|
12 // Don't execute the function if block_update_8002() got executed already,
|
Chris@0
|
13 // which used to do the same. Note: Its okay to check here, because
|
Chris@0
|
14 // update_do_one() does not update the installed schema version until the
|
Chris@0
|
15 // batch is finished.
|
Chris@0
|
16 $module_schema = drupal_get_installed_schema_version('block');
|
Chris@0
|
17
|
Chris@0
|
18 // The state entry 'block_update_8002_placeholder' is used in order to
|
Chris@0
|
19 // indicate that the placeholder block_update_8002() function has been
|
Chris@0
|
20 // executed, so this function needs to be executed as well. If the non
|
Chris@0
|
21 // placeholder version of block_update_8002() got executed already, the state
|
Chris@0
|
22 // won't be set and we skip this update.
|
Chris@0
|
23 if ($module_schema >= 8002 && !\Drupal::state()->get('block_update_8002_placeholder', FALSE)) {
|
Chris@0
|
24 return;
|
Chris@0
|
25 }
|
Chris@0
|
26
|
Chris@0
|
27 // Cleanup the state entry as its no longer needed.
|
Chris@0
|
28 \Drupal::state()->delete('block_update_8002');
|
Chris@0
|
29
|
Chris@0
|
30 $block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
|
Chris@0
|
31
|
Chris@0
|
32 $block_ids = array_keys($block_update_8001);
|
Chris@0
|
33 $block_storage = \Drupal::entityManager()->getStorage('block');
|
Chris@0
|
34 $blocks = $block_storage->loadMultiple($block_ids);
|
Chris@0
|
35 /** @var $blocks \Drupal\block\BlockInterface[] */
|
Chris@0
|
36 foreach ($blocks as $block) {
|
Chris@0
|
37 // This block has had conditions removed due to an inability to resolve
|
Chris@0
|
38 // contexts in block_update_8001() so disable it.
|
Chris@0
|
39
|
Chris@0
|
40 // Disable currently enabled blocks.
|
Chris@0
|
41 if ($block_update_8001[$block->id()]['status']) {
|
Chris@0
|
42 $block->setStatus(FALSE);
|
Chris@0
|
43 $block->save();
|
Chris@0
|
44 }
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 // Provides a list of plugin labels, keyed by plugin ID.
|
Chris@0
|
48 $condition_plugin_id_label_map = array_column(\Drupal::service('plugin.manager.condition')->getDefinitions(), 'label', 'id');
|
Chris@0
|
49
|
Chris@0
|
50 // Override with the UI labels we are aware of. Sadly they are not machine
|
Chris@0
|
51 // accessible, see
|
Chris@0
|
52 // \Drupal\node\Plugin\Condition\NodeType::buildConfigurationForm().
|
Chris@0
|
53 $condition_plugin_id_label_map['node_type'] = t('Content types');
|
Chris@0
|
54 $condition_plugin_id_label_map['request_path'] = t('Pages');
|
Chris@0
|
55 $condition_plugin_id_label_map['user_role'] = t('Roles');
|
Chris@0
|
56
|
Chris@0
|
57 if (count($block_ids) > 0) {
|
Chris@0
|
58 $message = t('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:');
|
Chris@0
|
59 $message .= '<ul>';
|
Chris@0
|
60 foreach ($blocks as $disabled_block_id => $disabled_block) {
|
Chris@0
|
61 $message .= '<li>' . t('@label (Visibility: @plugin_ids)', [
|
Chris@0
|
62 '@label' => $disabled_block->get('settings')['label'],
|
Chris@17
|
63 '@plugin_ids' => implode(', ', array_intersect_key($condition_plugin_id_label_map, array_flip(array_keys($block_update_8001[$disabled_block_id]['missing_context_ids'])))),
|
Chris@0
|
64 ]) . '</li>';
|
Chris@0
|
65 }
|
Chris@0
|
66 $message .= '</ul>';
|
Chris@0
|
67
|
Chris@0
|
68 return $message;
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Disable blocks that are placed into the "disabled" region.
|
Chris@0
|
74 */
|
Chris@0
|
75 function block_post_update_disabled_region_update() {
|
Chris@0
|
76 // An empty update will flush caches, forcing block_rebuild() to run.
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Fix invalid 'negate' values in block visibility conditions.
|
Chris@0
|
81 */
|
Chris@0
|
82 function block_post_update_fix_negate_in_conditions() {
|
Chris@0
|
83 $block_storage = \Drupal::entityTypeManager()->getStorage('block');
|
Chris@0
|
84 /** @var \Drupal\block\BlockInterface[] $blocks */
|
Chris@0
|
85 $blocks = $block_storage->loadMultiple();
|
Chris@0
|
86 foreach ($blocks as $block) {
|
Chris@0
|
87 $block_needs_saving = FALSE;
|
Chris@0
|
88 // Check each visibility condition for an invalid negate value, and fix it.
|
Chris@0
|
89 foreach ($block->getVisibilityConditions() as $condition_id => $condition) {
|
Chris@0
|
90 $configuration = $condition->getConfiguration();
|
Chris@0
|
91 if (array_key_exists('negate', $configuration) && !is_bool($configuration['negate'])) {
|
Chris@0
|
92 $configuration['negate'] = (bool) $configuration['negate'];
|
Chris@0
|
93 $condition->setConfiguration($configuration);
|
Chris@0
|
94 $block_needs_saving = TRUE;
|
Chris@0
|
95 }
|
Chris@0
|
96 }
|
Chris@0
|
97 if ($block_needs_saving) {
|
Chris@0
|
98 $block->save();
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|