function ModuleRouteSubscriber::alterRoutes

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php \Drupal\Core\EventSubscriber\ModuleRouteSubscriber::alterRoutes()
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php \Drupal\Core\EventSubscriber\ModuleRouteSubscriber::alterRoutes()
  3. 11.x core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php \Drupal\Core\EventSubscriber\ModuleRouteSubscriber::alterRoutes()

Overrides RouteSubscriberBase::alterRoutes

File

core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php, line 34

Class

ModuleRouteSubscriber
A route subscriber to remove routes that depend on modules being enabled.

Namespace

Drupal\Core\EventSubscriber

Code

protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection as $name => $route) {
        if ($route->hasRequirement('_module_dependencies')) {
            $modules = $route->getRequirement('_module_dependencies');
            $explode_and = $this->explodeString($modules, '+');
            if (count($explode_and) > 1) {
                foreach ($explode_and as $module) {
                    // If any moduleExists() call returns FALSE, remove the route and
                    // move on to the next.
                    if (!$this->moduleHandler
                        ->moduleExists($module)) {
                        $collection->remove($name);
                        continue 2;
                    }
                }
            }
            else {
                // OR condition, exploding on ',' character.
                foreach ($this->explodeString($modules, ',') as $module) {
                    if ($this->moduleHandler
                        ->moduleExists($module)) {
                        continue 2;
                    }
                }
                // If no modules are found, and we get this far, remove the route.
                $collection->remove($name);
            }
        }
    }
}

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