nortmas
4/24/2020 - 9:27 AM

Full HTML page early response

<?php

namespace Drupal\sigel_language\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Handles requests.
 */
class RequestSubscriber implements EventSubscriberInterface {

  /**
   * Request event handler.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The request event.
   */
  public function onRequest(GetResponseEvent $event) {
    // Allow only font page path with no language prefix.
    if ($event->getRequest()->getRequestUri() !== '/') {
      return;
    }

    $module_path = drupal_get_path('module', 'sigel_language');
    $twig = \Drupal::service('twig');
    $template = $twig->loadTemplate($module_path . '/templates/language-redirect.html.twig');

    $response = new Response($template->render([]));
    $event->setResponse($response);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // Set a high priority so it is executed before routing.
    $events[KernelEvents::REQUEST][] = ['onRequest', 1000];
    return $events;
  }

}