You are here

class PathAliasSubscriber in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/path_alias/src/EventSubscriber/PathAliasSubscriber.php \Drupal\path_alias\EventSubscriber\PathAliasSubscriber
  2. 9 core/modules/path_alias/src/EventSubscriber/PathAliasSubscriber.php \Drupal\path_alias\EventSubscriber\PathAliasSubscriber

Provides a path subscriber that converts path aliases.

Hierarchy

  • class \Drupal\path_alias\EventSubscriber\PathAliasSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PathAliasSubscriber

1 string reference to 'PathAliasSubscriber'
path_alias.services.yml in core/modules/path_alias/path_alias.services.yml
core/modules/path_alias/path_alias.services.yml
1 service uses PathAliasSubscriber
path_alias.subscriber in core/modules/path_alias/path_alias.services.yml
Drupal\path_alias\EventSubscriber\PathAliasSubscriber

File

core/modules/path_alias/src/EventSubscriber/PathAliasSubscriber.php, line 15

Namespace

Drupal\path_alias\EventSubscriber
View source
class PathAliasSubscriber implements EventSubscriberInterface {

  /**
   * The alias manager that caches alias lookups based on the request.
   *
   * @var \Drupal\path_alias\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * The current path.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

  /**
   * Constructs a new PathSubscriber instance.
   *
   * @param \Drupal\path_alias\AliasManagerInterface $alias_manager
   *   The alias manager.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path.
   */
  public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
    $this->aliasManager = $alias_manager;
    $this->currentPath = $current_path;
  }

  /**
   * Sets the cache key on the alias manager cache decorator.
   *
   * KernelEvents::CONTROLLER is used in order to be executed after routing.
   *
   * @param \Symfony\Component\HttpKernel\Event\ControllerEvent $event
   *   The Event to process.
   */
  public function onKernelController(ControllerEvent $event) {

    // Set the cache key on the alias manager cache decorator.
    if ($event
      ->isMainRequest()) {
      $this->aliasManager
        ->setCacheKey(rtrim($this->currentPath
        ->getPath($event
        ->getRequest()), '/'));
    }
  }

  /**
   * Ensures system paths for the request get cached.
   */
  public function onKernelTerminate(TerminateEvent $event) {
    $this->aliasManager
      ->writeCache();
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() : array {
    $events[KernelEvents::CONTROLLER][] = [
      'onKernelController',
      200,
    ];
    $events[KernelEvents::TERMINATE][] = [
      'onKernelTerminate',
      200,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathAliasSubscriber::$aliasManager protected property The alias manager that caches alias lookups based on the request.
PathAliasSubscriber::$currentPath protected property The current path.
PathAliasSubscriber::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
PathAliasSubscriber::onKernelController public function Sets the cache key on the alias manager cache decorator.
PathAliasSubscriber::onKernelTerminate public function Ensures system paths for the request get cached.
PathAliasSubscriber::__construct public function Constructs a new PathSubscriber instance.