RouteSubscriber.php in Zircon Profile 8
File
core/modules/views/src/EventSubscriber/RouteSubscriber.php
View source
<?php
namespace Drupal\views\EventSubscriber;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\views\Plugin\views\display\DisplayRouterInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
protected $viewsDisplayPairs;
protected $viewStorage;
protected $state;
protected $viewRouteNames = array();
public function __construct(EntityManagerInterface $entity_manager, StateInterface $state) {
$this->viewStorage = $entity_manager
->getStorage('view');
$this->state = $state;
}
public function reset() {
$this->viewsDisplayPairs = NULL;
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::FINISHED] = array(
'routeRebuildFinished',
);
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
-175,
];
return $events;
}
protected function getViewsDisplayIDsWithRoute() {
if (!isset($this->viewsDisplayPairs)) {
$this->viewsDisplayPairs = array();
$views = $this
->getApplicableViews();
foreach ($views as $data) {
list($view_id, $display_id) = $data;
$this->viewsDisplayPairs[] = $view_id . '.' . $display_id;
}
$this->viewsDisplayPairs = array_combine($this->viewsDisplayPairs, $this->viewsDisplayPairs);
}
return $this->viewsDisplayPairs;
}
public function routes() {
$collection = new RouteCollection();
foreach ($this
->getViewsDisplayIDsWithRoute() as $pair) {
list($view_id, $display_id) = explode('.', $pair);
$view = $this->viewStorage
->load($view_id);
if (($view = $view
->getExecutable()) && $view instanceof ViewExecutable) {
if ($view
->setDisplay($display_id) && ($display = $view->displayHandlers
->get($display_id))) {
if ($display instanceof DisplayRouterInterface) {
$this->viewRouteNames += (array) $display
->collectRoutes($collection);
}
}
$view
->destroy();
}
}
$this->state
->set('views.view_route_names', $this->viewRouteNames);
return $collection;
}
protected function alterRoutes(RouteCollection $collection) {
foreach ($this
->getViewsDisplayIDsWithRoute() as $pair) {
list($view_id, $display_id) = explode('.', $pair);
$view = $this->viewStorage
->load($view_id);
if (($view = $view
->getExecutable()) && $view instanceof ViewExecutable) {
if ($view
->setDisplay($display_id) && ($display = $view->displayHandlers
->get($display_id))) {
if ($display instanceof DisplayRouterInterface) {
$view_route_names = $display
->alterRoutes($collection);
$this->viewRouteNames = $view_route_names + $this->viewRouteNames;
foreach ($view_route_names as $id_display => $route_name) {
$view_route_name = $this->viewsDisplayPairs[$id_display];
unset($this->viewsDisplayPairs[$id_display]);
$collection
->remove("views.{$view_route_name}");
}
}
}
$view
->destroy();
}
}
}
public function routeRebuildFinished() {
$this
->reset();
$this->state
->set('views.view_route_names', $this->viewRouteNames);
}
protected function getApplicableViews() {
return Views::getApplicableViews('uses_route');
}
}