function TestPrepareLayout::onBeforePrepareLayout

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/modules/layout_builder_element_test/src/EventSubscriber/TestPrepareLayout.php \Drupal\layout_builder_element_test\EventSubscriber\TestPrepareLayout::onBeforePrepareLayout()
  2. 11.x core/modules/layout_builder/tests/modules/layout_builder_element_test/src/EventSubscriber/TestPrepareLayout.php \Drupal\layout_builder_element_test\EventSubscriber\TestPrepareLayout::onBeforePrepareLayout()

Subscriber to test acting before the LB subscriber.

Parameters

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

File

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

Class

TestPrepareLayout
Provides an event subscriber for testing section storage alteration.

Namespace

Drupal\layout_builder_element_test\EventSubscriber

Code

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();
    }
  }
}

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