field_layout.install 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Field Layout.
  5. */
  6. use Drupal\Core\Cache\Cache;
  7. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  8. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  9. use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
  10. /**
  11. * Implements hook_install().
  12. */
  13. function field_layout_install() {
  14. // Ensure each entity display has a layout.
  15. $entity_save = function (EntityDisplayWithLayoutInterface $entity) {
  16. $entity->ensureLayout()->save();
  17. };
  18. array_map($entity_save, EntityViewDisplay::loadMultiple());
  19. array_map($entity_save, EntityFormDisplay::loadMultiple());
  20. // Invalidate the render cache since all content will now have a layout.
  21. Cache::invalidateTags(['rendered']);
  22. }
  23. /**
  24. * Implements hook_uninstall().
  25. */
  26. function field_layout_uninstall() {
  27. // Reset each entity display to use the one-column layout to best approximate
  28. // the absence of layouts.
  29. $entity_save = function (EntityDisplayWithLayoutInterface $entity) {
  30. $entity->setLayoutId('layout_onecol')->save();
  31. };
  32. array_map($entity_save, EntityViewDisplay::loadMultiple());
  33. array_map($entity_save, EntityFormDisplay::loadMultiple());
  34. // Invalidate the render cache since all content will no longer have a layout.
  35. Cache::invalidateTags(['rendered']);
  36. }