PanelsEverywhereRouteSubscriber.php in Panels Everywhere 8.4
File
src/Routing/PanelsEverywhereRouteSubscriber.php
View source
<?php
namespace Drupal\panels_everywhere\Routing;
use Drupal\Core\Display\VariantInterface;
use Drupal\page_manager\PageVariantInterface;
use Drupal\page_manager\Routing\PageManagerRoutes;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteCompiler;
use Drupal\page_manager\PageInterface;
class PanelsEverywhereRouteSubscriber extends PageManagerRoutes {
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityStorage
->loadMultiple() as $entity_id => $entity) {
if (!$entity
->status() || !$entity
->getVariants()) {
continue;
}
$other_variants = [];
$panels_everywhere_variant_present = FALSE;
foreach ($entity
->getVariants() as $variant_id => $variant) {
if ($variant
->getVariantPluginId() != 'panels_everywhere_variant') {
$other_variants[$variant_id] = $variant;
continue;
}
$panels_everywhere_variant_present = TRUE;
$route = $this
->getRouteAndCleanup($entity, $variant, $collection);
if (is_null($route)) {
continue;
}
$route
->setDefault('page_id', $entity
->id());
}
if ($panels_everywhere_variant_present) {
foreach ($other_variants as $variant_id => $variant) {
$variant_route = $this
->getRouteFor($entity, $variant, $collection);
if ($variant_route) {
$variant_route
->setDefault('page_id', $entity
->id());
}
}
}
}
}
protected function getRouteAndCleanup(PageInterface $page, PageVariantInterface $variant, RouteCollection $collection) {
$page_id = $page
->id();
$variant_id = $variant
->id();
$route_name = "page_manager.page_view_{$page_id}_{$variant_id}";
$variantRoute = $this
->getRouteFor($page, $variant, $collection);
if (is_null($variantRoute)) {
return NULL;
}
if ($variant
->getVariantPlugin()
->isRouteOverrideEnabled()) {
return $variantRoute;
}
$route_name_original = $variantRoute
->getDefault('overridden_route_name');
$collection
->remove($route_name);
return $collection
->get($route_name_original);
}
protected function getRouteFor(PageInterface $page, PageVariantInterface $variant, RouteCollection $collection) {
$page_id = $page
->id();
$variant_id = $variant
->id();
$route_name = "page_manager.page_view_{$page_id}_{$variant_id}";
return $collection
->get($route_name);
}
}