function LayoutBuilderEntityViewDisplay::preSave

Same name in other branches
  1. 9 core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::preSave()
  2. 8.9.x core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::preSave()
  3. 10 core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::preSave()

Overrides EntityDisplayBase::preSave

File

core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php, line 124

Class

LayoutBuilderEntityViewDisplay
Provides an entity view display entity that has a layout.

Namespace

Drupal\layout_builder\Entity

Code

public function preSave(EntityStorageInterface $storage) {
    $original_value = isset($this->original) ? $this->original
        ->isOverridable() : FALSE;
    $new_value = $this->isOverridable();
    if ($original_value !== $new_value) {
        $entity_type_id = $this->getTargetEntityTypeId();
        $bundle = $this->getTargetBundle();
        if ($new_value) {
            $this->addSectionField($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME);
        }
        else {
            $this->removeSectionField($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME);
        }
    }
    parent::preSave($storage);
    $already_enabled = isset($this->original) ? $this->original
        ->isLayoutBuilderEnabled() : FALSE;
    $set_enabled = $this->isLayoutBuilderEnabled();
    if ($already_enabled !== $set_enabled) {
        if ($set_enabled) {
            // Loop through all existing field-based components and add them as
            // section-based components.
            $components = $this->getComponents();
            // Sort the components by weight.
            uasort($components, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
            foreach ($components as $name => $component) {
                $this->setComponent($name, $component);
            }
        }
        else {
            // When being disabled, remove all existing section data.
            $this->removeAllSections();
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.