You are here

public function PrepareLayout::onPrepareLayout in Layout builder library 8

Prepares a layout for use in the UI.

Parameters

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

File

src/EventSubscriber/PrepareLayout.php, line 57

Class

PrepareLayout
Alters a layout override to use layout library selection over the default.

Namespace

Drupal\layout_library\EventSubscriber

Code

public function onPrepareLayout(PrepareLayoutEvent $event) {
  $section_storage = $event
    ->getSectionStorage();

  // If the layout has pending changes, do nothing.
  if ($this->layoutTempstoreRepository
    ->has($section_storage)) {
    return;
  }
  elseif ($section_storage instanceof OverridesSectionStorageInterface && !$section_storage
    ->isOverridden()) {
    $entity = $section_storage
      ->getContextValue('entity');
    if ($entity instanceof FieldableEntityInterface && $entity
      ->hasField('layout_selection') && !$entity
      ->get('layout_selection')
      ->isEmpty()) {
      $layout = $entity
        ->get('layout_selection')->entity;
      if ($layout instanceof Layout) {
        $sections = $layout
          ->getLayout();
        foreach ($sections as $section) {
          $section_storage
            ->appendSection($section);
        }
        $this->layoutTempstoreRepository
          ->set($section_storage);
      }
    }
  }
}