Same filename and directory in other branches
  1. 8.9.x core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php
  2. 9 core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php

Namespace

Drupal\display_variant_test\EventSubscriber

File

core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php
View source
<?php

namespace Drupal\display_variant_test\EventSubscriber;

use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Render\PageDisplayVariantSelectionEvent;
use Drupal\Core\Render\RenderEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Selects the test page display variant.
 */
class TestPageDisplayVariantSubscriber implements EventSubscriberInterface {

  /**
   * Selects the page display variant.
   *
   * @param \Drupal\Core\Render\PageDisplayVariantSelectionEvent $event
   *   The event to process.
   */
  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;
  }

}

Classes

Namesort descending Description
TestPageDisplayVariantSubscriber Selects the test page display variant.