You are here

protected function RouteSubscriber::alterRoutes in Custom Permissions 8

Same name and namespace in other branches
  1. 8.2 src/Routing/RouteSubscriber.php \Drupal\config_perms\Routing\RouteSubscriber::alterRoutes()

Alters existing routes for a specific collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection for adding routes.

Overrides RouteSubscriberBase::alterRoutes

File

src/Routing/RouteSubscriber.php, line 20

Class

RouteSubscriber
Class RouteSubscriber.

Namespace

Drupal\config_perms\Routing

Code

protected function alterRoutes(RouteCollection $collection) {
  $custom_perms = CustomPermsEntity::loadMultiple();
  foreach ($custom_perms as $custom_perm) {
    if ($custom_perm
      ->getStatus()) {
      $paths = $this
        ->configPermsParsePath($custom_perm
        ->getPath());
      foreach ($paths as $path) {
        $path = $path[0] == '/' ? $path : '/' . $path;
        $url_object = \Drupal::service('path.validator')
          ->getUrlIfValidWithoutAccessCheck($path);
        if ($url_object) {
          $route_name = $url_object
            ->getRouteName();
          if ($route = $collection
            ->get($route_name)) {
            $route
              ->setRequirement('_permission', $custom_perm
              ->label());
          }
        }
      }
    }
  }
}