You are here

class AmpEventSubscriber in Accelerated Mobile Pages (AMP) 8.3

Redirects AMP requests to ?_wrapper_format=amp if appropriate.

Hierarchy

Expanded class hierarchy of AmpEventSubscriber

1 string reference to 'AmpEventSubscriber'
amp.services.yml in ./amp.services.yml
amp.services.yml
1 service uses AmpEventSubscriber
amp_event_subscriber in ./amp.services.yml
Drupal\amp\EventSubscriber\AmpEventSubscriber

File

src/EventSubscriber/AmpEventSubscriber.php, line 16

Namespace

Drupal\amp\EventSubscriber
View source
class AmpEventSubscriber extends ServiceProviderBase implements EventSubscriberInterface {

  /**
   * AMP context service.
   *
   * @var Drupal\amp\Routing\AmpContext
   */
  protected $ampContext;

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs an AmpEventSubscriber object.
   *
   * @param Drupal\amp\Routing\AmpContext $ampContext
   *   The AMP context service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
   *   The RouteMatch service.
   */
  public function __construct(AmpContext $ampContext, RouteMatchInterface $routeMatch) {
    $this->ampContext = $ampContext;
    $this->routeMatch = $routeMatch;
  }

  /**
   * Alters the wrapper format if this is an AMP request.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
   *   The event to process.
   */
  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');
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Run before main_content_view_subscriber.
    $events[KernelEvents::VIEW][] = [
      'onView',
      100,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpEventSubscriber::$ampContext protected property AMP context service.
AmpEventSubscriber::$routeMatch protected property The route match service.
AmpEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AmpEventSubscriber::onView public function Alters the wrapper format if this is an AMP request.
AmpEventSubscriber::__construct public function Constructs an AmpEventSubscriber object.
ServiceProviderBase::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter 5
ServiceProviderBase::register public function Registers services to the container. Overrides ServiceProviderInterface::register 1