public function TestPrepareLayout::onAfterPrepareLayout in Drupal 9
Subscriber to test acting after 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 102
Class
- TestPrepareLayout
- An event subscriber to test altering section storage via the \Drupal\layout_builder\Event\PrepareLayoutEvent.
Namespace
Drupal\layout_builder_element_test\EventSubscriberCode
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);
}
}
}