setContexts example


      $page_display
        ->setMainContent($main_content)
        ->setTitle($title)
        ->addCacheableDependency($event);
      // Some display variants need to be passed an array of contexts with       // values because they can't get all their contexts globally. For example,       // in Page Manager, you can create a Page which has a specific static       // context (e.g. a context that refers to the Node with nid 6), if any       // such contexts were added to the $event, pass them to the $page_display.       if ($page_display instanceof ContextAwareVariantInterface) {
        $page_display->setContexts($event->getContexts());
      }

      // Generate a #type => page render array using the page display variant,       // the page display will build the content for the various page regions.       $page = [
        '#type' => 'page',
      ];
      $page += $page_display->build();
    }

    // $page is now fully built. Find all non-empty page regions, and add a

  public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $event) {
    $event->setPluginId('display_variant_test');
    $event->setPluginConfiguration(['required_configuration' => 'A very important, required value.']);
    $event->addCacheTags(['custom_cache_tag']);

    $context = new Context(new ContextDefinition('string', NULL, TRUE), 'Explicitly passed in context.');
    $event->setContexts(['context' => $context]);
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    $events[RenderEvents::SELECT_PAGE_DISPLAY_VARIANT][] = ['onSelectPageDisplayVariant'];
    return $events;
  }

}
Home | Imprint | This part of the site doesn't use cookies.