You are here

public function AmpEventSubscriber::onView in Accelerated Mobile Pages (AMP) 8.3

Alters the wrapper format if this is an AMP request.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event: The event to process.

File

src/EventSubscriber/AmpEventSubscriber.php, line 51

Class

AmpEventSubscriber
Redirects AMP requests to ?_wrapper_format=amp if appropriate.

Namespace

Drupal\amp\EventSubscriber

Code

public function onView(GetResponseForControllerResultEvent $event) {

  // Don't interfere if this is a request that does not use html or amp
  // wrapper formats.
  $wrapper_format = isset($_GET['_wrapper_format']) ? $_GET['_wrapper_format'] : '';
  if (!empty($wrapper_format) && !in_array($wrapper_format, [
    'html',
    'amp',
  ])) {
    return;
  }

  // See if this is a request that already uses the wrapper.
  $amp_wrapper_format = isset($_GET['_wrapper_format']) && $_GET['_wrapper_format'] == 'amp';

  // See if this route and object are AMP, without checking the active theme.
  $isAmpRoute = $this->ampContext
    ->isAmpRoute($this->routeMatch, NULL, FALSE);

  // Get the current request.
  $request = $event
    ->getRequest();

  // Redirect requests that are not AMP routes to standard html processing.
  if (!$isAmpRoute) {
    $request->query
      ->set(MainContentViewSubscriber::WRAPPER_FORMAT, 'html');
  }
  elseif (!$amp_wrapper_format && $isAmpRoute) {
    $request->query
      ->set(MainContentViewSubscriber::WRAPPER_FORMAT, 'amp');
  }
}