3 namespace Drupal\views\EventSubscriber;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\Core\Routing\RouteSubscriberBase;
8 use Drupal\Core\Routing\RoutingEvents;
9 use Drupal\views\Plugin\views\display\DisplayRouterInterface;
10 use Drupal\views\ViewExecutable;
11 use Drupal\views\Views;
12 use Symfony\Component\Routing\RouteCollection;
15 * Builds up the routes of all views.
17 * The general idea is to execute first all alter hooks to determine which
18 * routes are overridden by views. This information is used to determine which
19 * views have to be added by views in the dynamic event.
22 * @see \Drupal\views\Plugin\views\display\PathPluginBase
24 class RouteSubscriber extends RouteSubscriberBase {
27 * Stores a list of view,display IDs which haven't be used in the alter event.
31 protected $viewsDisplayPairs;
36 * @var \Drupal\Core\Entity\EntityStorageInterface
38 protected $viewStorage;
41 * The state key value store.
43 * @var \Drupal\Core\State\StateInterface
48 * Stores an array of route names keyed by view_id.display_id.
52 protected $viewRouteNames = [];
55 * Constructs a \Drupal\views\EventSubscriber\RouteSubscriber instance.
57 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
59 * @param \Drupal\Core\State\StateInterface $state
60 * The state key value store.
62 public function __construct(EntityManagerInterface $entity_manager, StateInterface $state) {
63 $this->viewStorage = $entity_manager->getStorage('view');
64 $this->state = $state;
68 * Resets the internal state of the route subscriber.
70 public function reset() {
71 $this->viewsDisplayPairs = NULL;
77 public static function getSubscribedEvents() {
78 $events = parent::getSubscribedEvents();
79 $events[RoutingEvents::FINISHED] = ['routeRebuildFinished'];
80 // Ensure to run after the entity resolver subscriber
81 // @see \Drupal\Core\EventSubscriber\EntityRouteAlterSubscriber
82 $events[RoutingEvents::ALTER] = ['onAlterRoutes', -175];
88 * Gets all the views and display IDs using a route.
90 protected function getViewsDisplayIDsWithRoute() {
91 if (!isset($this->viewsDisplayPairs)) {
92 $this->viewsDisplayPairs = [];
94 // @todo Convert this method to some service.
95 $views = $this->getApplicableViews();
96 foreach ($views as $data) {
97 list($view_id, $display_id) = $data;
98 $this->viewsDisplayPairs[] = $view_id . '.' . $display_id;
100 $this->viewsDisplayPairs = array_combine($this->viewsDisplayPairs, $this->viewsDisplayPairs);
102 return $this->viewsDisplayPairs;
106 * Returns a set of route objects.
108 * @return \Symfony\Component\Routing\RouteCollection
109 * A route collection.
111 public function routes() {
112 $collection = new RouteCollection();
113 foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
114 list($view_id, $display_id) = explode('.', $pair);
115 $view = $this->viewStorage->load($view_id);
116 // @todo This should have an executable factory injected.
117 if (($view = $view->getExecutable()) && $view instanceof ViewExecutable) {
118 if ($view->setDisplay($display_id) && $display = $view->displayHandlers->get($display_id)) {
119 if ($display instanceof DisplayRouterInterface) {
120 $this->viewRouteNames += (array) $display->collectRoutes($collection);
127 $this->state->set('views.view_route_names', $this->viewRouteNames);
134 protected function alterRoutes(RouteCollection $collection) {
135 foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
136 list($view_id, $display_id) = explode('.', $pair);
137 $view = $this->viewStorage->load($view_id);
138 // @todo This should have an executable factory injected.
139 if (($view = $view->getExecutable()) && $view instanceof ViewExecutable) {
140 if ($view->setDisplay($display_id) && $display = $view->displayHandlers->get($display_id)) {
141 if ($display instanceof DisplayRouterInterface) {
142 // If the display returns TRUE a route item was found, so it does not
144 $view_route_names = $display->alterRoutes($collection);
145 $this->viewRouteNames = $view_route_names + $this->viewRouteNames;
146 foreach ($view_route_names as $id_display => $route_name) {
147 $view_route_name = $this->viewsDisplayPairs[$id_display];
148 unset($this->viewsDisplayPairs[$id_display]);
149 $collection->remove("views.$view_route_name");
159 * Stores the new route names after they have been rebuilt.
161 * Callback for the RoutingEvents::FINISHED event.
163 * @see \Drupal\views\EventSubscriber::getSubscribedEvents()
165 public function routeRebuildFinished() {
167 $this->state->set('views.view_route_names', $this->viewRouteNames);
171 * Returns all views/display combinations with routes.
173 * @see \Drupal\views\Views::getApplicableViews()
175 protected function getApplicableViews() {
176 return Views::getApplicableViews('uses_route');