ThemeTestSubscriber.php 3.03 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\theme_test\EventSubscriber;

5
use Drupal\Core\Render\RendererInterface;
6
use Drupal\Core\Url;
7
use Drupal\Core\Routing\RouteMatchInterface;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
use Symfony\Component\HttpKernel\Event\RequestEvent;
10 11 12 13 14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Theme test subscriber for controller requests.
 */
15
class ThemeTestSubscriber implements EventSubscriberInterface {
16 17 18 19

  /**
   * The used container.
   *
20
   * @todo This variable is never initialized, so we don't know what it is.
21
   *   See https://www.drupal.org/node/2721315
22 23
   */
  protected $container;
24

25 26 27 28 29 30 31
  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $currentRouteMatch;

32 33 34 35 36 37 38
  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

39 40 41 42
  /**
   * Constructs a new ThemeTestSubscriber.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
43 44
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
45
   */
46
  public function __construct(RouteMatchInterface $current_route_match, RendererInterface $renderer) {
47
    $this->currentRouteMatch = $current_route_match;
48
    $this->renderer = $renderer;
49
  }
50

51 52 53 54 55
  /**
   * Generates themed output early in a page request.
   *
   * @see \Drupal\system\Tests\Theme\ThemeEarlyInitializationTest::testRequestListener()
   */
56
  public function onRequest(RequestEvent $event) {
57
    if ($this->currentRouteMatch->getRouteName() === 'theme_test.request_listener') {
58 59 60 61 62 63 64 65 66
      // First, force the theme registry to be rebuilt on this page request.
      // This allows us to test a full initialization of the theme system in
      // the code below.
      drupal_theme_rebuild();
      // Next, initialize the theme system by storing themed text in a global
      // variable. We will use this later in
      // theme_test_request_listener_page_callback() to test that even when the
      // theme system is initialized this early, it is still capable of
      // returning output and theming the page as a whole.
67
      $more_link = [
68
        '#type' => 'more_link',
69
        '#url' => Url::fromRoute('user.page'),
70 71
        '#attributes' => ['title' => 'Themed output generated in a KernelEvents::REQUEST listener'],
      ];
72
      $GLOBALS['theme_test_output'] = $this->renderer->renderPlain($more_link);
73
    }
74 75 76 77 78
  }

  /**
   * Ensures that the theme registry was not initialized.
   */
79
  public function onView(RequestEvent $event) {
80
    $current_route = $this->currentRouteMatch->getRouteName();
81
    $entity_autocomplete_route = [
82
      'system.entity_autocomplete',
83
    ];
84

85
    if (in_array($current_route, $entity_autocomplete_route)) {
86 87 88
      if ($this->container->initialized('theme.registry')) {
        throw new \Exception('registry initialized');
      }
89 90 91 92 93 94
    }
  }

  /**
   * {@inheritdoc}
   */
95
  public static function getSubscribedEvents() {
96 97
    $events[KernelEvents::REQUEST][] = ['onRequest'];
    $events[KernelEvents::VIEW][] = ['onView', -1000];
98 99 100 101
    return $events;
  }

}