system.post_update.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for System.
  5. */
  6. use Drupal\Core\Config\Entity\ConfigEntityUpdater;
  7. use Drupal\Core\Entity\Display\EntityDisplayInterface;
  8. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  9. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  10. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  11. /**
  12. * Re-save all configuration entities to recalculate dependencies.
  13. */
  14. function system_post_update_recalculate_configuration_entity_dependencies(&$sandbox = NULL) {
  15. if (!isset($sandbox['config_names'])) {
  16. $sandbox['config_names'] = \Drupal::configFactory()->listAll();
  17. $sandbox['count'] = count($sandbox['config_names']);
  18. }
  19. /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  20. $config_manager = \Drupal::service('config.manager');
  21. $count = 0;
  22. foreach ($sandbox['config_names'] as $key => $config_name) {
  23. if ($entity = $config_manager->loadConfigEntityByName($config_name)) {
  24. $entity->save();
  25. }
  26. unset($sandbox['config_names'][$key]);
  27. $count++;
  28. // Do 50 at a time.
  29. if ($count == 50) {
  30. break;
  31. }
  32. }
  33. $sandbox['#finished'] = empty($sandbox['config_names']) ? 1 : ($sandbox['count'] - count($sandbox['config_names'])) / $sandbox['count'];
  34. return t('Configuration dependencies recalculated');
  35. }
  36. /**
  37. * Update entity displays to contain the region for each field.
  38. */
  39. function system_post_update_add_region_to_entity_displays() {
  40. $entity_save = function (EntityDisplayInterface $entity) {
  41. // preSave() will fill in the correct region based on the 'type'.
  42. $entity->save();
  43. };
  44. array_map($entity_save, EntityViewDisplay::loadMultiple());
  45. array_map($entity_save, EntityFormDisplay::loadMultiple());
  46. }
  47. /**
  48. * Force caches using hashes to be cleared (Twig, render cache, etc.).
  49. */
  50. function system_post_update_hashes_clear_cache() {
  51. // Empty post-update hook.
  52. }
  53. /**
  54. * Force plugin definitions to be cleared.
  55. *
  56. * @see https://www.drupal.org/node/2802663
  57. */
  58. function system_post_update_timestamp_plugins() {
  59. // Empty post-update hook.
  60. }
  61. /**
  62. * Clear caches to ensure Classy's message library is always added.
  63. */
  64. function system_post_update_classy_message_library() {
  65. // Empty post-update hook.
  66. }
  67. /**
  68. * Force field type plugin definitions to be cleared.
  69. *
  70. * @see https://www.drupal.org/node/2403703
  71. */
  72. function system_post_update_field_type_plugins() {
  73. // Empty post-update hook.
  74. }
  75. /**
  76. * Clear caches due to schema changes in core.entity.schema.yml.
  77. */
  78. function system_post_update_field_formatter_entity_schema() {
  79. // Empty post-update hook.
  80. }
  81. /**
  82. * Change plugin IDs of actions.
  83. */
  84. function system_post_update_change_action_plugins() {
  85. $old_new_action_id_map = [
  86. 'comment_publish_action' => 'entity:publish_action:comment',
  87. 'comment_unpublish_action' => 'entity:unpublish_action:comment',
  88. 'comment_save_action' => 'entity:save_action:comment',
  89. 'node_publish_action' => 'entity:publish_action:node',
  90. 'node_unpublish_action' => 'entity:unpublish_action:node',
  91. 'node_save_action' => 'entity:save_action:node',
  92. ];
  93. /** @var \Drupal\system\Entity\Action[] $actions */
  94. $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  95. foreach ($actions as $action) {
  96. if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
  97. $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
  98. $action->save();
  99. }
  100. }
  101. }
  102. /**
  103. * Change plugin IDs of delete actions.
  104. */
  105. function system_post_update_change_delete_action_plugins() {
  106. $old_new_action_id_map = [
  107. 'comment_delete_action' => 'entity:delete_action:comment',
  108. 'node_delete_action' => 'entity:delete_action:node',
  109. ];
  110. /** @var \Drupal\system\Entity\Action[] $actions */
  111. $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  112. foreach ($actions as $action) {
  113. if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
  114. $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
  115. $action->save();
  116. }
  117. }
  118. }
  119. /**
  120. * Force cache clear for language item callback.
  121. *
  122. * @see https://www.drupal.org/node/2851736
  123. */
  124. function system_post_update_language_item_callback() {
  125. // Empty post-update hook.
  126. }
  127. /**
  128. * Update all entity displays that contain extra fields.
  129. */
  130. function system_post_update_extra_fields(&$sandbox = NULL) {
  131. $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  132. $entity_field_manager = \Drupal::service('entity_field.manager');
  133. $callback = function (EntityDisplayInterface $display) use ($entity_field_manager) {
  134. $display_context = $display instanceof EntityViewDisplayInterface ? 'display' : 'form';
  135. $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
  136. // If any extra fields are used as a component, resave the display with the
  137. // updated component information.
  138. $needs_save = FALSE;
  139. if (!empty($extra_fields[$display_context])) {
  140. foreach ($extra_fields[$display_context] as $name => $extra_field) {
  141. if ($component = $display->getComponent($name)) {
  142. $display->setComponent($name, $component);
  143. $needs_save = TRUE;
  144. }
  145. }
  146. }
  147. return $needs_save;
  148. };
  149. $config_entity_updater->update($sandbox, 'entity_form_display', $callback);
  150. $config_entity_updater->update($sandbox, 'entity_view_display', $callback);
  151. }