block.install 4.2 KB

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