field_layout.install 1.45 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Contains install and update functions for Field Layout.
 */

use Drupal\Core\Cache\Cache;
9
use Drupal\Core\Entity\Display\EntityDisplayInterface;
10 11 12 13 14 15 16 17 18
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;

/**
 * Implements hook_install().
 */
function field_layout_install() {
  // Ensure each entity display has a layout.
19 20 21 22
  $entity_save = function (EntityDisplayInterface $entity) {
    if ($entity instanceof EntityDisplayWithLayoutInterface) {
      $entity->ensureLayout()->save();
    }
23 24 25 26 27 28 29 30 31 32 33 34 35 36
  };
  array_map($entity_save, EntityViewDisplay::loadMultiple());
  array_map($entity_save, EntityFormDisplay::loadMultiple());

  // Invalidate the render cache since all content will now have a layout.
  Cache::invalidateTags(['rendered']);
}

/**
 * Implements hook_uninstall().
 */
function field_layout_uninstall() {
  // Reset each entity display to use the one-column layout to best approximate
  // the absence of layouts.
37 38 39 40
  $entity_save = function (EntityDisplayInterface $entity) {
    if ($entity instanceof EntityDisplayWithLayoutInterface) {
      $entity->setLayoutId('layout_onecol')->save();
    }
41 42 43 44 45 46 47
  };
  array_map($entity_save, EntityViewDisplay::loadMultiple());
  array_map($entity_save, EntityFormDisplay::loadMultiple());

  // Invalidate the render cache since all content will no longer have a layout.
  Cache::invalidateTags(['rendered']);
}