class PrepareLayout

Same name in other branches
  1. 9 core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php \Drupal\layout_builder\EventSubscriber\PrepareLayout
  2. 10 core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php \Drupal\layout_builder\EventSubscriber\PrepareLayout

An event subscriber to prepare section storage.

Section storage works via the \Drupal\layout_builder\Event\PrepareLayoutEvent.

Hierarchy

  • class \Drupal\layout_builder\EventSubscriber\PrepareLayout implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait

Expanded class hierarchy of PrepareLayout

See also

\Drupal\layout_builder\Event\PrepareLayoutEvent

\Drupal\layout_builder\Element\LayoutBuilder::prepareLayout()

1 string reference to 'PrepareLayout'
layout_builder.services.yml in core/modules/layout_builder/layout_builder.services.yml
core/modules/layout_builder/layout_builder.services.yml
1 service uses PrepareLayout
layout_builder.element.prepare_layout in core/modules/layout_builder/layout_builder.services.yml
Drupal\layout_builder\EventSubscriber\PrepareLayout

File

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

Namespace

Drupal\layout_builder\EventSubscriber
View source
class PrepareLayout implements EventSubscriberInterface {
    use StringTranslationTrait;
    
    /**
     * The layout tempstore repository.
     *
     * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
     */
    protected $layoutTempstoreRepository;
    
    /**
     * The messenger service.
     *
     * @var \Drupal\Core\Messenger\MessengerInterface
     */
    protected $messenger;
    
    /**
     * Constructs a new PrepareLayout.
     *
     * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
     *   The tempstore repository.
     * @param \Drupal\Core\Messenger\MessengerInterface $messenger
     *   The messenger service.
     */
    public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
        $this->layoutTempstoreRepository = $layout_tempstore_repository;
        $this->messenger = $messenger;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[LayoutBuilderEvents::PREPARE_LAYOUT][] = [
            'onPrepareLayout',
            10,
        ];
        return $events;
    }
    
    /**
     * Prepares a layout for use in the UI.
     *
     * @param \Drupal\layout_builder\Event\PrepareLayoutEvent $event
     *   The prepare layout event.
     */
    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);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
PrepareLayout::$layoutTempstoreRepository protected property The layout tempstore repository.
PrepareLayout::$messenger protected property The messenger service.
PrepareLayout::getSubscribedEvents public static function
PrepareLayout::onPrepareLayout public function Prepares a layout for use in the UI.
PrepareLayout::__construct public function Constructs a new PrepareLayout.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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