You are here

class RouteSubscriber 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

Customizes Views routes.

Hierarchy

  • class \Drupal\Core\Routing\RouteSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
views_advanced_routing.services.yml in ./views_advanced_routing.services.yml
views_advanced_routing.services.yml
1 service uses RouteSubscriber
views_advanced_routing.route_subscriber in ./views_advanced_routing.services.yml
Drupal\views_advanced_routing\Routing\RouteSubscriber

File

src/Routing/RouteSubscriber.php, line 20
Contains \Drupal\views_advanced_routing\Routing\RouteSubscriber.

Namespace

Drupal\views_advanced_routing\Routing
View source
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * The view storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $viewStorage;

  /**
   * Constructs a RouteSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->viewStorage = $entity_manager
      ->getStorage('view');
  }

  /**
   * {@inheritdoc}
   */
  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();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$viewStorage protected property The view storage.
RouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::__construct public function Constructs a RouteSubscriber object.
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1