You are here

public function ThemeTestSubscriber::onRequest in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/theme_test/src/EventSubscriber/ThemeTestSubscriber.php \Drupal\theme_test\EventSubscriber\ThemeTestSubscriber::onRequest()

Generates themed output early in a page request.

See also

\Drupal\system\Tests\Theme\ThemeEarlyInitializationTest::testRequestListener()

File

core/modules/system/tests/modules/theme_test/src/EventSubscriber/ThemeTestSubscriber.php, line 56

Class

ThemeTestSubscriber
Theme test subscriber for controller requests.

Namespace

Drupal\theme_test\EventSubscriber

Code

public function onRequest(RequestEvent $event) {
  if ($this->currentRouteMatch
    ->getRouteName() === 'theme_test.request_listener') {

    // 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.
    $more_link = [
      '#type' => 'more_link',
      '#url' => Url::fromRoute('user.page'),
      '#attributes' => [
        'title' => 'Themed output generated in a KernelEvents::REQUEST listener',
      ],
    ];
    $GLOBALS['theme_test_output'] = $this->renderer
      ->renderPlain($more_link);
  }
}