system.post_update.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for System.
  5. */
  6. use Drupal\Core\Entity\Display\EntityDisplayInterface;
  7. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  8. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  9. /**
  10. * Re-save all configuration entities to recalculate dependencies.
  11. */
  12. function system_post_update_recalculate_configuration_entity_dependencies(&$sandbox = NULL) {
  13. if (!isset($sandbox['config_names'])) {
  14. $sandbox['config_names'] = \Drupal::configFactory()->listAll();
  15. $sandbox['count'] = count($sandbox['config_names']);
  16. }
  17. /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  18. $config_manager = \Drupal::service('config.manager');
  19. $count = 0;
  20. foreach ($sandbox['config_names'] as $key => $config_name) {
  21. if ($entity = $config_manager->loadConfigEntityByName($config_name)) {
  22. $entity->save();
  23. }
  24. unset($sandbox['config_names'][$key]);
  25. $count++;
  26. // Do 50 at a time.
  27. if ($count == 50) {
  28. break;
  29. }
  30. }
  31. $sandbox['#finished'] = empty($sandbox['config_names']) ? 1 : ($sandbox['count'] - count($sandbox['config_names'])) / $sandbox['count'];
  32. return t('Configuration dependencies recalculated');
  33. }
  34. /**
  35. * Update entity displays to contain the region for each field.
  36. */
  37. function system_post_update_add_region_to_entity_displays() {
  38. $entity_save = function (EntityDisplayInterface $entity) {
  39. // preSave() will fill in the correct region based on the 'type'.
  40. $entity->save();
  41. };
  42. array_map($entity_save, EntityViewDisplay::loadMultiple());
  43. array_map($entity_save, EntityFormDisplay::loadMultiple());
  44. }
  45. /**
  46. * Force caches using hashes to be cleared (Twig, render cache, etc.).
  47. */
  48. function system_post_update_hashes_clear_cache() {
  49. // Empty post-update hook.
  50. }
  51. /**
  52. * Force plugin definitions to be cleared.
  53. *
  54. * @see https://www.drupal.org/node/2802663
  55. */
  56. function system_post_update_timestamp_plugins() {
  57. // Empty post-update hook.
  58. }
  59. /**
  60. * Clear caches to ensure Classy's message library is always added.
  61. */
  62. function system_post_update_classy_message_library() {
  63. // Empty post-update hook.
  64. }
  65. /**
  66. * Force field type plugin definitions to be cleared.
  67. *
  68. * @see https://www.drupal.org/node/2403703
  69. */
  70. function system_post_update_field_type_plugins() {
  71. // Empty post-update hook.
  72. }
  73. /**
  74. * Clear caches due to schema changes in core.entity.schema.yml.
  75. */
  76. function system_post_update_field_formatter_entity_schema() {
  77. // Empty post-update hook.
  78. }
  79. /**
  80. * Change plugin IDs of actions.
  81. */
  82. function system_post_update_change_action_plugins() {
  83. $old_new_action_id_map = [
  84. 'comment_publish_action' => 'entity:publish_action:comment',
  85. 'comment_unpublish_action' => 'entity:unpublish_action:comment',
  86. 'comment_save_action' => 'entity:save_action:comment',
  87. 'node_publish_action' => 'entity:publish_action:node',
  88. 'node_unpublish_action' => 'entity:unpublish_action:node',
  89. 'node_save_action' => 'entity:save_action:node',
  90. ];
  91. /** @var \Drupal\system\Entity\Action[] $actions */
  92. $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  93. foreach ($actions as $action) {
  94. if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
  95. $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
  96. $action->save();
  97. }
  98. }
  99. }