function PrepareLayout::onPrepareLayout

Same name in other branches
  1. 10 core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php \Drupal\layout_builder\EventSubscriber\PrepareLayout::onPrepareLayout()
  2. 11.x core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php \Drupal\layout_builder\EventSubscriber\PrepareLayout::onPrepareLayout()

Prepares a layout for use in the UI.

Parameters

\Drupal\layout_builder\Event\PrepareLayoutEvent $event: The prepare layout event.

File

core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php, line 67

Class

PrepareLayout
An event subscriber to prepare section storage.

Namespace

Drupal\layout_builder\EventSubscriber

Code

public function onPrepareLayout(PrepareLayoutEvent $event) {
    $section_storage = $event->getSectionStorage();
    // If the layout has pending changes, add a warning.
    if ($this->layoutTempstoreRepository
        ->has($section_storage)) {
        $this->messenger
            ->addWarning($this->t('You have unsaved changes.'));
    }
    else {
        // If the layout is an override that has not yet been overridden, copy the
        // sections from the corresponding default.
        if ($section_storage instanceof OverridesSectionStorageInterface && !$section_storage->isOverridden()) {
            $sections = $section_storage->getDefaultSectionStorage()
                ->getSections();
            foreach ($sections as $section) {
                $section_storage->appendSection($section);
            }
        }
        // Add storage to tempstore regardless of what the storage is.
        $this->layoutTempstoreRepository
            ->set($section_storage);
    }
}

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