block.post_update.php 4.0 KB

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