You are here

class PrepareLayout in Layout builder library 8

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

@package Drupal\layout_library\EventSubscriber

Hierarchy

  • class \Drupal\layout_library\EventSubscriber\PrepareLayout implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PrepareLayout

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

File

src/EventSubscriber/PrepareLayout.php, line 18

Namespace

Drupal\layout_library\EventSubscriber
View source
class PrepareLayout implements EventSubscriberInterface {

  /**
   * The layout tempstore repository.
   *
   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   */
  protected $layoutTempstoreRepository;

  /**
   * PrepareLayout constructor.
   *
   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   *   The tempstore repository.
   */
  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
    $this->layoutTempstoreRepository = $layout_tempstore_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Only available in >= D9.1, see
    // https://www.drupal.org/project/layout_library/issues/3082434.
    if (class_exists('Drupal\\layout_builder\\Event\\PrepareLayoutEvent')) {

      // Priority higher than Layout Builder to interact first.
      $events[LayoutBuilderEvents::PREPARE_LAYOUT] = [
        'onPrepareLayout',
        20,
      ];
      return $events;
    }
    return [];
  }

  /**
   * 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, 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);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrepareLayout::$layoutTempstoreRepository protected property The layout tempstore repository.
PrepareLayout::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PrepareLayout::onPrepareLayout public function Prepares a layout for use in the UI.
PrepareLayout::__construct public function PrepareLayout constructor.