You are here

class AliasSubscriber in Mini site 8

Class AliasSubscriber.

Listener to process request controller information.

@package Drupal\minisite\EventSubscriber

Hierarchy

  • class \Drupal\minisite\EventSubscriber\AliasSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AliasSubscriber

1 string reference to 'AliasSubscriber'
minisite.services.yml in ./minisite.services.yml
minisite.services.yml
1 service uses AliasSubscriber
minisite.subscriber in ./minisite.services.yml
Drupal\minisite\EventSubscriber\AliasSubscriber

File

src/EventSubscriber/AliasSubscriber.php, line 18

Namespace

Drupal\minisite\EventSubscriber
View source
class AliasSubscriber implements EventSubscriberInterface {

  /**
   * Set Minisite delivery controller if request URI matches asset alias.
   *
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Event that is created to create a response for a request.
   */
  public function onRequestSetController(GetResponseEvent $event) {

    // Do not alter non-master request (this is a case when an exception is
    // thrown in controller).
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $request = $event
      ->getRequest();

    // Load asset by the request URI which is an asset alias. This call must
    // be as "lightweight" as possible as it will run before any other routes
    // are considered (it is still faster to run this before all RouterListener
    // processing).
    $parsed_uri = UrlHelper::parse($request
      ->getRequestUri());
    $asset = Asset::loadByAlias($parsed_uri['path']);
    if ($asset) {
      $request->attributes
        ->set('_controller', '\\Drupal\\minisite\\Controller\\AliasController::deliverAsset');
      $request->attributes
        ->set('asset_id', $asset
        ->id());

      // Stop further propagation as our raw URL has matched.
      $event
        ->stopPropagation();
    }
  }

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

    // The RouterListener has priority 32, and we need to run before that
    // because we are assessing raw URL path (we do not have a route for
    // asset aliases).
    $events[KernelEvents::REQUEST][] = [
      'onRequestSetController',
      33,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AliasSubscriber::onRequestSetController public function Set Minisite delivery controller if request URI matches asset alias.