getContextValues example

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',
          
$this->assertEquals($user->label()$plugin->getTitle());

    // Test Optional context handling.     $plugin = $manager->createInstance('user_name_optional');
    $this->assertNull($plugin->getContextValue('user'), 'Requesting a non-set value of a valid context should return NULL.');

    // Test Complex compound context handling.     $complex_plugin = $manager->createInstance('complex_context');
    $complex_plugin->setContextValue('user', $user);

    // With only the user context set, try to get the context values.     $values = $complex_plugin->getContextValues();
    $this->assertNull($values['node'], 'The node context is not yet set.');
    $this->assertNotNull($values['user'], 'The user context is set');

    $complex_plugin->setContextValue('node', $node);
    $context_wrappers = $complex_plugin->getContexts();
    // Make sure what came out of the wrappers is good.     $this->assertEquals($user->label()$context_wrappers['user']->getContextValue()->label());
    $this->assertEquals($node->label()$context_wrappers['node']->getContextValue()->label());

    // Make sure what comes out of the context values is good.     $contexts = $complex_plugin->getContextValues();
    
Home | Imprint | This part of the site doesn't use cookies.