block.install 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Block.
  5. */
  6. use Drupal\Core\Cache\Cache;
  7. /**
  8. * Implements hook_install().
  9. */
  10. function block_install() {
  11. // Because the Block module upon installation unconditionally overrides all
  12. // HTML output by selecting a different page display variant, we must
  13. // invalidate all cached HTML output.
  14. Cache::invalidateTags(['rendered']);
  15. }
  16. /**
  17. * Update block visibility context mapping.
  18. */
  19. function block_update_8001() {
  20. // This update function updates blocks for the change from
  21. // https://www.drupal.org/node/2354889.
  22. // Core visibility context plugins are updated automatically; blocks with
  23. // unknown plugins are disabled and their previous visibility settings are
  24. // saved in key value storage; see change record
  25. // https://www.drupal.org/node/2527840 for more explanation.
  26. // These are all the contexts that Drupal core provides.
  27. $context_service_id_map = [
  28. 'node.node' => '@node.node_route_context:node',
  29. 'user.current_user' => '@user.current_user_context:current_user',
  30. ];
  31. foreach (array_keys(\Drupal::languageManager()->getDefinedLanguageTypesInfo()) as $language_type_id) {
  32. $context_service_id_map['language.' . $language_type_id] = '@language.current_language_context:' . $language_type_id;
  33. }
  34. // Contributed modules should leverage hook_update_dependencies() in order to
  35. // be executed after block_update_8001(). The blocks are then disabled if the
  36. // contexts are still missing via
  37. // block_post_update_disable_blocks_with_missing_contexts().
  38. $config_factory = \Drupal::configFactory();
  39. $backup_values = $update_backup = [];
  40. foreach ($config_factory->listAll('block.block.') as $block_config_name) {
  41. $block = $config_factory->getEditable($block_config_name);
  42. if ($visibility = $block->get('visibility')) {
  43. foreach ($visibility as $condition_plugin_id => &$condition) {
  44. foreach ($condition['context_mapping'] as $key => $context) {
  45. if (!isset($context_service_id_map[$context])) {
  46. // Remove the visibility condition for unknown context mapping
  47. // entries, so the update process itself runs through and users can
  48. // fix their block placements manually OR alternatively contributed
  49. // modules can run their own update functions to update mappings
  50. // that they provide.
  51. $backup_values[$context][] = $condition_plugin_id;
  52. unset($visibility[$condition_plugin_id]);
  53. continue;
  54. }
  55. // Replace the context ID based on the defined mapping.
  56. $condition['context_mapping'][$key] = $context_service_id_map[$context];
  57. }
  58. }
  59. $block->set('visibility', $visibility);
  60. if ($backup_values) {
  61. // We not only store the missing context mappings but also the previous
  62. // block status, in order to allow contributed and custom modules to do
  63. // their own updates.
  64. $update_backup[$block->get('id')] = [
  65. 'missing_context_ids' => $backup_values,
  66. 'status' => $block->get('status')
  67. ];
  68. }
  69. }
  70. // Mark the resulting configuration as trusted data. This avoids issues with
  71. // future schema changes.
  72. $block->save(TRUE);
  73. }
  74. if ($update_backup) {
  75. \Drupal::keyValue('update_backup')->set('block_update_8001', $update_backup);
  76. }
  77. return t('Block context IDs updated.');
  78. }
  79. /**
  80. * Placeholder for the previous 8002 update.
  81. */
  82. function block_update_8002() {
  83. \Drupal::state()->set('block_update_8002_placeholder', TRUE);
  84. }
  85. /**
  86. * Remove 'cache' setting.
  87. */
  88. function block_update_8003() {
  89. $config_factory = \Drupal::configFactory();
  90. foreach ($config_factory->listAll('block.block.') as $block_config_name) {
  91. $block = $config_factory->getEditable($block_config_name);
  92. // Remove the 'cache' setting.
  93. $settings = $block->get('settings');
  94. unset($settings['cache']);
  95. $block->set('settings', $settings);
  96. // Mark the resulting configuration as trusted data. This avoids issues with
  97. // future schema changes.
  98. $block->save(TRUE);
  99. }
  100. return t('Block settings updated.');
  101. }