block.post_update.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for Block.
  5. */
  6. /**
  7. * @addtogroup updates-8.0.0-beta
  8. * @{
  9. */
  10. /**
  11. * Disable all blocks with missing context IDs in block_update_8001().
  12. */
  13. function block_post_update_disable_blocks_with_missing_contexts() {
  14. // Don't execute the function if block_update_8002() got executed already,
  15. // which used to do the same. Note: Its okay to check here, because
  16. // update_do_one() does not update the installed schema version until the
  17. // batch is finished.
  18. $module_schema = drupal_get_installed_schema_version('block');
  19. // The state entry 'block_update_8002_placeholder' is used in order to
  20. // indicate that the placeholder block_update_8002() function has been
  21. // executed, so this function needs to be executed as well. If the non
  22. // placeholder version of block_update_8002() got executed already, the state
  23. // won't be set and we skip this update.
  24. if ($module_schema >= 8002 && !\Drupal::state()->get('block_update_8002_placeholder', FALSE)) {
  25. return;
  26. }
  27. // Cleanup the state entry as its no longer needed.
  28. \Drupal::state()->delete('block_update_8002');
  29. $block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
  30. $block_ids = array_keys($block_update_8001);
  31. $block_storage = \Drupal::entityManager()->getStorage('block');
  32. $blocks = $block_storage->loadMultiple($block_ids);
  33. /** @var $blocks \Drupal\block\BlockInterface[] */
  34. foreach ($blocks as $block) {
  35. // This block has had conditions removed due to an inability to resolve
  36. // contexts in block_update_8001() so disable it.
  37. // Disable currently enabled blocks.
  38. if ($block_update_8001[$block->id()]['status']) {
  39. $block->setStatus(FALSE);
  40. $block->save();
  41. }
  42. }
  43. // Provides a list of plugin labels, keyed by plugin ID.
  44. $condition_plugin_id_label_map = array_column(\Drupal::service('plugin.manager.condition')->getDefinitions(), 'label', 'id');
  45. // Override with the UI labels we are aware of. Sadly they are not machine
  46. // accessible, see
  47. // \Drupal\node\Plugin\Condition\NodeType::buildConfigurationForm().
  48. $condition_plugin_id_label_map['node_type'] = t('Content types');
  49. $condition_plugin_id_label_map['request_path'] = t('Pages');
  50. $condition_plugin_id_label_map['user_role'] = t('Roles');
  51. if (count($block_ids) > 0) {
  52. $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:');
  53. $message .= '<ul>';
  54. foreach ($blocks as $disabled_block_id => $disabled_block) {
  55. $message .= '<li>' . t('@label (Visibility: @plugin_ids)', array(
  56. '@label' => $disabled_block->get('settings')['label'],
  57. '@plugin_ids' => implode(', ', array_intersect_key($condition_plugin_id_label_map, array_flip(array_keys($block_update_8001[$disabled_block_id]['missing_context_ids']))))
  58. )) . '</li>';
  59. }
  60. $message .= '</ul>';
  61. return $message;
  62. }
  63. }
  64. /**
  65. * @} End of "addtogroup updates-8.0.0-beta".
  66. */