3 namespace Drupal\theme_test\EventSubscriber;
5 use Drupal\Core\Render\RendererInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 * Theme test subscriber for controller requests.
15 class ThemeTestSubscriber implements EventSubscriberInterface {
20 * @todo This variable is never initialized, so we don't know what it is.
21 * See https://www.drupal.org/node/2721315
26 * The current route match.
28 * @var \Drupal\Core\Routing\RouteMatchInterface
30 protected $currentRouteMatch;
35 * @var \Drupal\Core\Render\RendererInterface
40 * Constructs a new ThemeTestSubscriber.
42 * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
43 * @param \Drupal\Core\Render\RendererInterface $renderer
46 public function __construct(RouteMatchInterface $current_route_match, RendererInterface $renderer) {
47 $this->currentRouteMatch = $current_route_match;
48 $this->renderer = $renderer;
52 * Generates themed output early in a page request.
54 * @see \Drupal\system\Tests\Theme\ThemeEarlyInitializationTest::testRequestListener()
56 public function onRequest(GetResponseEvent $event) {
57 if ($this->currentRouteMatch->getRouteName() === 'theme_test.request_listener') {
58 // First, force the theme registry to be rebuilt on this page request.
59 // This allows us to test a full initialization of the theme system in
61 drupal_theme_rebuild();
62 // Next, initialize the theme system by storing themed text in a global
63 // variable. We will use this later in
64 // theme_test_request_listener_page_callback() to test that even when the
65 // theme system is initialized this early, it is still capable of
66 // returning output and theming the page as a whole.
68 '#type' => 'more_link',
69 '#url' => Url::fromRoute('user.page'),
70 '#attributes' => ['title' => 'Themed output generated in a KernelEvents::REQUEST listener'],
72 $GLOBALS['theme_test_output'] = $this->renderer->renderPlain($more_link);
77 * Ensures that the theme registry was not initialized.
79 public function onView(GetResponseEvent $event) {
80 $current_route = $this->currentRouteMatch->getRouteName();
81 $entity_autcomplete_route = [
82 'system.entity_autocomplete',
85 if (in_array($current_route, $entity_autcomplete_route)) {
86 if ($this->container->initialized('theme.registry')) {
87 throw new \Exception('registry initialized');
95 public static function getSubscribedEvents() {
96 $events[KernelEvents::REQUEST][] = ['onRequest'];
97 $events[KernelEvents::VIEW][] = ['onView', -1000];