system.post_update.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
  12. /**
  13. * Re-save all configuration entities to recalculate dependencies.
  14. */
  15. function system_post_update_recalculate_configuration_entity_dependencies(&$sandbox = NULL) {
  16. if (!isset($sandbox['config_names'])) {
  17. $sandbox['config_names'] = \Drupal::configFactory()->listAll();
  18. $sandbox['count'] = count($sandbox['config_names']);
  19. }
  20. /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  21. $config_manager = \Drupal::service('config.manager');
  22. $count = 0;
  23. foreach ($sandbox['config_names'] as $key => $config_name) {
  24. if ($entity = $config_manager->loadConfigEntityByName($config_name)) {
  25. $entity->save();
  26. }
  27. unset($sandbox['config_names'][$key]);
  28. $count++;
  29. // Do 50 at a time.
  30. if ($count == 50) {
  31. break;
  32. }
  33. }
  34. $sandbox['#finished'] = empty($sandbox['config_names']) ? 1 : ($sandbox['count'] - count($sandbox['config_names'])) / $sandbox['count'];
  35. return t('Configuration dependencies recalculated');
  36. }
  37. /**
  38. * Update entity displays to contain the region for each field.
  39. */
  40. function system_post_update_add_region_to_entity_displays() {
  41. $entity_save = function (EntityDisplayInterface $entity) {
  42. // preSave() will fill in the correct region based on the 'type'.
  43. $entity->save();
  44. };
  45. array_map($entity_save, EntityViewDisplay::loadMultiple());
  46. array_map($entity_save, EntityFormDisplay::loadMultiple());
  47. }
  48. /**
  49. * Force caches using hashes to be cleared (Twig, render cache, etc.).
  50. */
  51. function system_post_update_hashes_clear_cache() {
  52. // Empty post-update hook.
  53. }
  54. /**
  55. * Force plugin definitions to be cleared.
  56. *
  57. * @see https://www.drupal.org/node/2802663
  58. */
  59. function system_post_update_timestamp_plugins() {
  60. // Empty post-update hook.
  61. }
  62. /**
  63. * Clear caches to ensure Classy's message library is always added.
  64. */
  65. function system_post_update_classy_message_library() {
  66. // Empty post-update hook.
  67. }
  68. /**
  69. * Force field type plugin definitions to be cleared.
  70. *
  71. * @see https://www.drupal.org/node/2403703
  72. */
  73. function system_post_update_field_type_plugins() {
  74. // Empty post-update hook.
  75. }
  76. /**
  77. * Clear caches due to schema changes in core.entity.schema.yml.
  78. */
  79. function system_post_update_field_formatter_entity_schema() {
  80. // Empty post-update hook.
  81. }
  82. /**
  83. * Clear the library cache and ensure aggregate files are regenerated.
  84. */
  85. function system_post_update_fix_jquery_extend() {
  86. // Empty post-update hook.
  87. }
  88. /**
  89. * Change plugin IDs of actions.
  90. */
  91. function system_post_update_change_action_plugins() {
  92. $old_new_action_id_map = [
  93. 'comment_publish_action' => 'entity:publish_action:comment',
  94. 'comment_unpublish_action' => 'entity:unpublish_action:comment',
  95. 'comment_save_action' => 'entity:save_action:comment',
  96. 'node_publish_action' => 'entity:publish_action:node',
  97. 'node_unpublish_action' => 'entity:unpublish_action:node',
  98. 'node_save_action' => 'entity:save_action:node',
  99. ];
  100. /** @var \Drupal\system\Entity\Action[] $actions */
  101. $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  102. foreach ($actions as $action) {
  103. if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
  104. $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
  105. $action->save();
  106. }
  107. }
  108. }
  109. /**
  110. * Change plugin IDs of delete actions.
  111. */
  112. function system_post_update_change_delete_action_plugins() {
  113. $old_new_action_id_map = [
  114. 'comment_delete_action' => 'entity:delete_action:comment',
  115. 'node_delete_action' => 'entity:delete_action:node',
  116. ];
  117. /** @var \Drupal\system\Entity\Action[] $actions */
  118. $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  119. foreach ($actions as $action) {
  120. if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
  121. $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
  122. $action->save();
  123. }
  124. }
  125. }
  126. /**
  127. * Force cache clear for language item callback.
  128. *
  129. * @see https://www.drupal.org/node/2851736
  130. */
  131. function system_post_update_language_item_callback() {
  132. // Empty post-update hook.
  133. }
  134. /**
  135. * Update all entity view displays that contain extra fields.
  136. */
  137. function system_post_update_extra_fields(&$sandbox = NULL) {
  138. $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  139. $entity_field_manager = \Drupal::service('entity_field.manager');
  140. $callback = function (EntityDisplayInterface $display) use ($entity_field_manager) {
  141. $display_context = $display instanceof EntityViewDisplayInterface ? 'display' : 'form';
  142. $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
  143. // If any extra fields are used as a component, resave the display with the
  144. // updated component information.
  145. $needs_save = FALSE;
  146. if (!empty($extra_fields[$display_context])) {
  147. foreach ($extra_fields[$display_context] as $name => $extra_field) {
  148. if ($component = $display->getComponent($name)) {
  149. $display->setComponent($name, $component);
  150. $needs_save = TRUE;
  151. }
  152. }
  153. }
  154. return $needs_save;
  155. };
  156. $config_entity_updater->update($sandbox, 'entity_view_display', $callback);
  157. }
  158. /**
  159. * Update all entity form displays that contain extra fields.
  160. */
  161. function system_post_update_extra_fields_form_display(&$sandbox = NULL) {
  162. $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  163. $entity_field_manager = \Drupal::service('entity_field.manager');
  164. $callback = function (EntityDisplayInterface $display) use ($entity_field_manager) {
  165. $display_context = $display instanceof EntityViewDisplayInterface ? 'display' : 'form';
  166. $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
  167. // If any extra fields are used as a component, resave the display with the
  168. // updated component information.
  169. $needs_save = FALSE;
  170. if (!empty($extra_fields[$display_context])) {
  171. foreach ($extra_fields[$display_context] as $name => $extra_field) {
  172. if ($component = $display->getComponent($name)) {
  173. $display->setComponent($name, $component);
  174. $needs_save = TRUE;
  175. }
  176. }
  177. }
  178. return $needs_save;
  179. };
  180. $config_entity_updater->update($sandbox, 'entity_form_display', $callback);
  181. }
  182. /**
  183. * Force cache clear to ensure aggregated JavaScript files are regenerated.
  184. *
  185. * @see https://www.drupal.org/project/drupal/issues/2995570
  186. */
  187. function system_post_update_states_clear_cache() {
  188. // Empty post-update hook.
  189. }
  190. /**
  191. * Initialize 'expand_all_items' values to system_menu_block.
  192. */
  193. function system_post_update_add_expand_all_items_key_in_system_menu_block(&$sandbox = NULL) {
  194. if (!\Drupal::moduleHandler()->moduleExists('block')) {
  195. return;
  196. }
  197. \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'block', function ($block) {
  198. return strpos($block->getPluginId(), 'system_menu_block:') === 0;
  199. });
  200. }
  201. /**
  202. * Clear the menu cache.
  203. *
  204. * @see https://www.drupal.org/project/drupal/issues/3044364
  205. */
  206. function system_post_update_clear_menu_cache() {
  207. // Empty post-update hook.
  208. }
  209. /**
  210. * Clear the schema cache.
  211. */
  212. function system_post_update_layout_plugin_schema_change() {
  213. // Empty post-update hook.
  214. }
  215. /**
  216. * Populate the new 'match_limit' setting for the ER autocomplete widget.
  217. */
  218. function system_post_update_entity_reference_autocomplete_match_limit(&$sandbox = NULL) {
  219. $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  220. /** @var \Drupal\Core\Field\WidgetPluginManager $field_widget_manager */
  221. $field_widget_manager = \Drupal::service('plugin.manager.field.widget');
  222. $callback = function (EntityDisplayInterface $display) use ($field_widget_manager) {
  223. foreach ($display->getComponents() as $field_name => $component) {
  224. if (empty($component['type'])) {
  225. continue;
  226. }
  227. $plugin_definition = $field_widget_manager->getDefinition($component['type'], FALSE);
  228. if (is_a($plugin_definition['class'], EntityReferenceAutocompleteWidget::class, TRUE)) {
  229. return TRUE;
  230. }
  231. }
  232. return FALSE;
  233. };
  234. $config_entity_updater->update($sandbox, 'entity_form_display', $callback);
  235. }