You are here

public function TestPrepareLayout::onBeforePrepareLayout in Drupal 9

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 69

Class

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

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