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(): array { $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; } }