layout_builder.install 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Layout Builder.
  5. */
  6. use Drupal\Core\Cache\Cache;
  7. use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
  8. use Drupal\layout_builder\Section;
  9. /**
  10. * Implements hook_install().
  11. */
  12. function layout_builder_install() {
  13. $displays = LayoutBuilderEntityViewDisplay::loadMultiple();
  14. /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $displays */
  15. foreach ($displays as $display) {
  16. // Create the first section from any existing Field Layout settings.
  17. $field_layout = $display->getThirdPartySettings('field_layout');
  18. if (isset($field_layout['id'])) {
  19. $field_layout += ['settings' => []];
  20. $display->appendSection(new Section($field_layout['id'], $field_layout['settings']));
  21. }
  22. // Sort the components by weight.
  23. $components = $display->get('content');
  24. uasort($components, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
  25. foreach ($components as $name => $component) {
  26. $display->setComponent($name, $component);
  27. }
  28. $display->save();
  29. }
  30. // Clear the rendered cache to ensure the new layout builder flow is used.
  31. // While in many cases the above change will not affect the rendered output,
  32. // the cacheability metadata will have changed and should be processed to
  33. // prepare for future changes.
  34. Cache::invalidateTags(['rendered']);
  35. }