layout_builder.post_update.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @file
  4. * Post update functions for Layout Builder.
  5. */
  6. use Drupal\Core\Config\Entity\ConfigEntityUpdater;
  7. use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
  8. /**
  9. * Rebuild plugin dependencies for all entity view displays.
  10. */
  11. function layout_builder_post_update_rebuild_plugin_dependencies(&$sandbox = NULL) {
  12. $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  13. if (!isset($sandbox['ids'])) {
  14. $sandbox['ids'] = $storage->getQuery()->accessCheck(FALSE)->execute();
  15. $sandbox['count'] = count($sandbox['ids']);
  16. }
  17. for ($i = 0; $i < 10 && count($sandbox['ids']); $i++) {
  18. $id = array_shift($sandbox['ids']);
  19. if ($display = $storage->load($id)) {
  20. $display->save();
  21. }
  22. }
  23. $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
  24. }
  25. /**
  26. * Ensure all extra fields are properly stored on entity view displays.
  27. *
  28. * Previously
  29. * \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()
  30. * was not correctly setting the configuration for extra fields. This function
  31. * calls setComponent() for all extra field components to ensure the updated
  32. * logic is invoked on all extra fields to correct the settings.
  33. */
  34. function layout_builder_post_update_add_extra_fields(&$sandbox = NULL) {
  35. $entity_field_manager = \Drupal::service('entity_field.manager');
  36. \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function (LayoutEntityDisplayInterface $display) use ($entity_field_manager) {
  37. if (!$display->isLayoutBuilderEnabled()) {
  38. return FALSE;
  39. }
  40. $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
  41. $components = $display->getComponents();
  42. // Sort the components to avoid them being reordered by setComponent().
  43. uasort($components, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
  44. $result = FALSE;
  45. foreach ($components as $name => $component) {
  46. if (isset($extra_fields['display'][$name])) {
  47. $display->setComponent($name, $component);
  48. $result = TRUE;
  49. }
  50. }
  51. return $result;
  52. });
  53. }