class PathRootsSubscriber

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php \Drupal\Core\EventSubscriber\PathRootsSubscriber
  2. 10 core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php \Drupal\Core\EventSubscriber\PathRootsSubscriber
  3. 11.x core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php \Drupal\Core\EventSubscriber\PathRootsSubscriber

Provides all available first bits of all route paths.

Hierarchy

  • class \Drupal\Core\EventSubscriber\PathRootsSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PathRootsSubscriber

1 file declares its use of PathRootsSubscriber
PathRootsSubscriberTest.php in core/tests/Drupal/Tests/Core/EventSubscriber/PathRootsSubscriberTest.php
1 string reference to 'PathRootsSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses PathRootsSubscriber
router.path_roots_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\PathRootsSubscriber

File

core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php, line 13

Namespace

Drupal\Core\EventSubscriber
View source
class PathRootsSubscriber implements EventSubscriberInterface {
    
    /**
     * Stores the path roots available in the router.
     *
     * A path root is the first virtual directory of a path, like 'admin', 'node'
     * or 'user'.
     *
     * @var array
     */
    protected $pathRoots = [];
    
    /**
     * The state key value store.
     *
     * @var \Drupal\Core\State\StateInterface
     */
    protected $state;
    
    /**
     * Constructs a new PathRootsSubscriber instance.
     *
     * @param \Drupal\Core\State\StateInterface $state
     *   The state key value store.
     */
    public function __construct(StateInterface $state) {
        $this->state = $state;
    }
    
    /**
     * Collects all path roots.
     *
     * @param \Drupal\Core\Routing\RouteBuildEvent $event
     *   The route build event.
     */
    public function onRouteAlter(RouteBuildEvent $event) {
        $collection = $event->getRouteCollection();
        foreach ($collection->all() as $route) {
            $bits = explode('/', ltrim($route->getPath(), '/'));
            $this->pathRoots[$bits[0]] = $bits[0];
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function onRouteFinished() {
        $this->state
            ->set('router.path_roots', array_keys($this->pathRoots));
        $this->pathRoots = [];
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        $events = [];
        // Try to set a low priority to ensure that all routes are already added.
        $events[RoutingEvents::ALTER][] = [
            'onRouteAlter',
            -1024,
        ];
        $events[RoutingEvents::FINISHED][] = [
            'onRouteFinished',
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
PathRootsSubscriber::$pathRoots protected property Stores the path roots available in the router.
PathRootsSubscriber::$state protected property The state key value store.
PathRootsSubscriber::getSubscribedEvents public static function
PathRootsSubscriber::onRouteAlter public function Collects all path roots.
PathRootsSubscriber::onRouteFinished public function
PathRootsSubscriber::__construct public function Constructs a new PathRootsSubscriber instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.