public function LayoutBuilderEntityViewDisplay::preSave in Drupal 10
Same name and namespace in other branches
- 8 core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::preSave()
- 9 core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::preSave()
Acts on an entity before the presave hook is invoked.
Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.
Parameters
\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.
Throws
\Exception When there is a problem that should prevent saving the entity.
Overrides EntityDisplayBase::preSave
See also
\Drupal\Core\Field\FieldItemListInterface::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\EntityCode
public function preSave(EntityStorageInterface $storage) {
parent::preSave($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);
}
}
$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();
}
}
}