You are here

protected function RouteSubscriber::alterRoutes in Views Advanced Routing 8

Same name and namespace in other branches
  1. 2.x src/Routing/RouteSubscriber.php \Drupal\views_advanced_routing\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 42
Contains \Drupal\views_advanced_routing\Routing\RouteSubscriber.

Class

RouteSubscriber
Customizes Views routes.

Namespace

Drupal\views_advanced_routing\Routing

Code

protected function alterRoutes(RouteCollection $collection) {
  $views = [];
  foreach (Views::getApplicableViews('uses_route') as $data) {
    list($view_id, $display_id) = $data;
    $views[$view_id][] = $display_id;
  }
  foreach ($views as $view_id => $displays) {

    /** @var \Drupal\views\Entity\View $view */
    $view = $this->viewStorage
      ->load($view_id);
    foreach ($displays as $display_id) {
      if (($vex = $view
        ->getExecutable()) && $vex instanceof ViewExecutable) {
        if ($vex
          ->setDisplay($display_id) && ($display = $vex->displayHandlers
          ->get($display_id))) {
          if ($display instanceof DisplayRouterInterface) {
            $options = $display
              ->getOption('display_extenders');
            if (isset($options['views_advanced_routing_route'])) {
              $route = $collection
                ->get($display
                ->getRouteName());
              $settings = $options['views_advanced_routing_route'];
              if ($route && !empty($settings['route'])) {
                $route_info = $settings['route'];
                $route_info += [
                  'defaults' => [],
                  'requirements' => [],
                  'options' => [],
                ];
                $route
                  ->addOptions($route_info['options'])
                  ->addRequirements($route_info['requirements'])
                  ->addDefaults($route_info['defaults']);
                $collection
                  ->add($display
                  ->getRouteName(), $route);
              }
            }
          }
        }
        $vex
          ->destroy();
      }
    }
  }
}