field_layout.install 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Display\EntityDisplayInterface;
  8. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  9. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  10. use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
  11. /**
  12. * Implements hook_install().
  13. */
  14. function field_layout_install() {
  15. // Ensure each entity display has a layout.
  16. $entity_save = function (EntityDisplayInterface $entity) {
  17. if ($entity instanceof EntityDisplayWithLayoutInterface) {
  18. $entity->ensureLayout()->save();
  19. }
  20. };
  21. array_map($entity_save, EntityViewDisplay::loadMultiple());
  22. array_map($entity_save, EntityFormDisplay::loadMultiple());
  23. // Invalidate the render cache since all content will now have a layout.
  24. Cache::invalidateTags(['rendered']);
  25. }
  26. /**
  27. * Implements hook_uninstall().
  28. */
  29. function field_layout_uninstall() {
  30. // Reset each entity display to use the one-column layout to best approximate
  31. // the absence of layouts.
  32. $entity_save = function (EntityDisplayInterface $entity) {
  33. if ($entity instanceof EntityDisplayWithLayoutInterface) {
  34. $entity->setLayoutId('layout_onecol')->save();
  35. }
  36. };
  37. array_map($entity_save, EntityViewDisplay::loadMultiple());
  38. array_map($entity_save, EntityFormDisplay::loadMultiple());
  39. // Invalidate the render cache since all content will no longer have a layout.
  40. Cache::invalidateTags(['rendered']);
  41. }