5 * Post update functions for Block.
9 * Disable all blocks with missing context IDs in block_update_8001().
11 function block_post_update_disable_blocks_with_missing_contexts() {
12 // Don't execute the function if block_update_8002() got executed already,
13 // which used to do the same. Note: Its okay to check here, because
14 // update_do_one() does not update the installed schema version until the
16 $module_schema = drupal_get_installed_schema_version('block');
18 // The state entry 'block_update_8002_placeholder' is used in order to
19 // indicate that the placeholder block_update_8002() function has been
20 // executed, so this function needs to be executed as well. If the non
21 // placeholder version of block_update_8002() got executed already, the state
22 // won't be set and we skip this update.
23 if ($module_schema >= 8002 && !\Drupal::state()->get('block_update_8002_placeholder', FALSE)) {
27 // Cleanup the state entry as its no longer needed.
28 \Drupal::state()->delete('block_update_8002');
30 $block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
32 $block_ids = array_keys($block_update_8001);
33 $block_storage = \Drupal::entityManager()->getStorage('block');
34 $blocks = $block_storage->loadMultiple($block_ids);
35 /** @var $blocks \Drupal\block\BlockInterface[] */
36 foreach ($blocks as $block) {
37 // This block has had conditions removed due to an inability to resolve
38 // contexts in block_update_8001() so disable it.
40 // Disable currently enabled blocks.
41 if ($block_update_8001[$block->id()]['status']) {
42 $block->setStatus(FALSE);
47 // Provides a list of plugin labels, keyed by plugin ID.
48 $condition_plugin_id_label_map = array_column(\Drupal::service('plugin.manager.condition')->getDefinitions(), 'label', 'id');
50 // Override with the UI labels we are aware of. Sadly they are not machine
52 // \Drupal\node\Plugin\Condition\NodeType::buildConfigurationForm().
53 $condition_plugin_id_label_map['node_type'] = t('Content types');
54 $condition_plugin_id_label_map['request_path'] = t('Pages');
55 $condition_plugin_id_label_map['user_role'] = t('Roles');
57 if (count($block_ids) > 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:');
60 foreach ($blocks as $disabled_block_id => $disabled_block) {
61 $message .= '<li>' . t('@label (Visibility: @plugin_ids)', [
62 '@label' => $disabled_block->get('settings')['label'],
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']))))
73 * Disable blocks that are placed into the "disabled" region.
75 function block_post_update_disabled_region_update() {
76 // An empty update will flush caches, forcing block_rebuild() to run.
80 * Fix invalid 'negate' values in block visibility conditions.
82 function block_post_update_fix_negate_in_conditions() {
83 $block_storage = \Drupal::entityTypeManager()->getStorage('block');
84 /** @var \Drupal\block\BlockInterface[] $blocks */
85 $blocks = $block_storage->loadMultiple();
86 foreach ($blocks as $block) {
87 $block_needs_saving = FALSE;
88 // Check each visibility condition for an invalid negate value, and fix it.
89 foreach ($block->getVisibilityConditions() as $condition_id => $condition) {
90 $configuration = $condition->getConfiguration();
91 if (array_key_exists('negate', $configuration) && !is_bool($configuration['negate'])) {
92 $configuration['negate'] = (bool) $configuration['negate'];
93 $condition->setConfiguration($configuration);
94 $block_needs_saving = TRUE;
97 if ($block_needs_saving) {