PageManagerRoutes.php in Page Manager 8.4
File
src/Routing/PageManagerRoutes.php
View source
<?php
namespace Drupal\page_manager\Routing;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteCompiler;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\page_manager\PageInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class PageManagerRoutes extends RouteSubscriberBase {
protected $entityStorage;
protected $cacheTagsInvalidator;
public function __construct(EntityTypeManagerInterface $entity_type_manager, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
$this->entityStorage = $entity_type_manager
->getStorage('page');
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityStorage
->loadMultiple() as $entity_id => $entity) {
if (!$entity
->status() || !$entity
->getVariants()) {
continue;
}
$defaults = [];
$parameters = [];
$requirements = [];
$route_name = "page_manager.page_view_{$entity_id}";
if ($overridden_route_name = $this
->findOverriddenRouteName($entity, $collection)) {
$base_route_name = $overridden_route_name;
$collection_route = $collection
->get($overridden_route_name);
$defaults['overridden_route_name'] = $overridden_route_name;
$path = $collection_route
->getPath();
$parameters = $collection_route
->getOption('parameters') ?: [];
$requirements = $collection_route
->getRequirements();
}
else {
$base_route_name = $route_name;
$path = $entity
->getPath();
}
foreach ($entity
->getParameters() as $parameter_name => $parameter) {
if (!empty($parameter['type'])) {
$parameters[$parameter_name]['type'] = $parameter['type'];
}
}
$defaults['base_route_name'] = $base_route_name;
$defaults['_entity_view'] = 'page_manager_page_variant';
$defaults['_title'] = $entity
->label();
$defaults['page_manager_page'] = $entity
->id();
$parameters['page_manager_page_variant']['type'] = 'entity:page_variant';
$parameters['page_manager_page']['type'] = 'entity:page';
$requirements['_page_access'] = 'page_manager_page.view';
foreach ($entity
->getVariants() as $variant_id => $variant) {
$route = new Route($path, $defaults + [
'page_manager_page_variant' => $variant_id,
'page_manager_page_variant_weight' => $variant
->getWeight(),
], $requirements, [
'parameters' => $parameters,
'_admin_route' => $entity
->usesAdminTheme(),
]);
$collection
->add($route_name . '_' . $variant_id, $route);
}
$this->cacheTagsInvalidator
->invalidateTags([
"page_manager_route_name:{$base_route_name}",
]);
}
}
protected function findOverriddenRouteName(PageInterface $entity, RouteCollection $collection) {
$path = $entity
->getPath();
foreach ($collection
->all() as $name => $collection_route) {
$route_path = $collection_route
->getPath();
$route_path_outline = RouteCompiler::getPatternOutline($route_path);
if (($path === $route_path || $path === $route_path_outline) && (!$collection_route
->getMethods() || in_array('GET', $collection_route
->getMethods())) && !$collection_route
->hasRequirement('_format')) {
return $name;
}
}
}
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER][] = [
'onAlterRoutes',
-160,
];
return $events;
}
}