You are here

class TestPrepareLayout in Drupal 9

An event subscriber to test altering section storage via the \Drupal\layout_builder\Event\PrepareLayoutEvent.

Hierarchy

Expanded class hierarchy of TestPrepareLayout

See also

\Drupal\layout_builder\Event\PrepareLayoutEvent

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

1 string reference to 'TestPrepareLayout'
layout_builder_element_test.services.yml in core/modules/layout_builder/tests/modules/layout_builder_element_test/layout_builder_element_test.services.yml
core/modules/layout_builder/tests/modules/layout_builder_element_test/layout_builder_element_test.services.yml
1 service uses TestPrepareLayout
layout_builder_element_test.prepare_layout in core/modules/layout_builder/tests/modules/layout_builder_element_test/layout_builder_element_test.services.yml
Drupal\layout_builder_element_test\EventSubscriber\TestPrepareLayout

File

core/modules/layout_builder/tests/modules/layout_builder_element_test/src/EventSubscriber/TestPrepareLayout.php, line 21

Namespace

Drupal\layout_builder_element_test\EventSubscriber
View source
class TestPrepareLayout 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 TestPrepareLayout.
   *
   * @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() {

    // Act before core's layout builder subscriber.
    $events[LayoutBuilderEvents::PREPARE_LAYOUT][] = [
      'onBeforePrepareLayout',
      20,
    ];

    // Act after core's layout builder subscriber.
    $events[LayoutBuilderEvents::PREPARE_LAYOUT][] = [
      'onAfterPrepareLayout',
      -10,
    ];
    return $events;
  }

  /**
   * Subscriber to test acting before the LB subscriber.
   *
   * @param \Drupal\layout_builder\Event\PrepareLayoutEvent $event
   *   The prepare layout event.
   */
  public function onBeforePrepareLayout(PrepareLayoutEvent $event) {
    $section_storage = $event
      ->getSectionStorage();
    $context = $section_storage
      ->getContextValues();
    if (!empty($context['entity'])) {

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $entity = $context['entity'];

      // Node 1 or 2: Append a block to the layout.
      if (in_array($entity
        ->id(), [
        '1',
        '2',
      ])) {
        $section = new Section('layout_onecol');
        $section
          ->appendComponent(new SectionComponent('fake-uuid', 'content', [
          'id' => 'static_block',
          'label' => 'Test static block title',
          'label_display' => 'visible',
          'provider' => 'fake_provider',
        ]));
        $section_storage
          ->appendSection($section);
      }

      // Node 2: Stop event propagation.
      if ($entity
        ->id() === '2') {
        $event
          ->stopPropagation();
      }
    }
  }

  /**
   * Subscriber to test acting after the LB subscriber.
   *
   * @param \Drupal\layout_builder\Event\PrepareLayoutEvent $event
   *   The prepare layout event.
   */
  public function onAfterPrepareLayout(PrepareLayoutEvent $event) {
    $section_storage = $event
      ->getSectionStorage();
    $context = $section_storage
      ->getContextValues();
    if (!empty($context['entity'])) {

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $entity = $context['entity'];

      // Node 1, 2, or 3: Append a block to the layout.
      if (in_array($entity
        ->id(), [
        '1',
        '2',
        '3',
      ])) {
        $section = new Section('layout_onecol');
        $section
          ->appendComponent(new SectionComponent('fake-uuid', 'content', [
          'id' => 'static_block_two',
          'label' => 'Test second static block title',
          'label_display' => 'visible',
          'provider' => 'fake_provider',
        ]));
        $section_storage
          ->appendSection($section);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
TestPrepareLayout::$layoutTempstoreRepository protected property The layout tempstore repository.
TestPrepareLayout::$messenger protected property The messenger service.
TestPrepareLayout::getSubscribedEvents public static function
TestPrepareLayout::onAfterPrepareLayout public function Subscriber to test acting after the LB subscriber.
TestPrepareLayout::onBeforePrepareLayout public function Subscriber to test acting before the LB subscriber.
TestPrepareLayout::__construct public function Constructs a new TestPrepareLayout.