RouteSubscriber.php in Page Manager 8.4
File
tests/modules/page_manager_routing_test/src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\page_manager_routing_test\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Route;
class RouteSubscriber implements EventSubscriberInterface {
public function beforePageManagerRoutes(RouteBuildEvent $event) {
$collection = $event
->getRouteCollection();
$route = new Route('/entity_test/{entity_test}', [], [
'_access' => 'TRUE',
]);
$route
->setRequirement('_format', 'xml');
$collection
->add('entity.entity_test.canonical.xml', $route);
}
public function afterPageManagerRoutes(RouteBuildEvent $event) {
$collection = $event
->getRouteCollection();
if ($original_route = $collection
->get('entity.entity_test.canonical')) {
$route = new Route($original_route
->getPath(), $original_route
->getDefaults(), $original_route
->getRequirements(), $original_route
->getOptions());
$route
->setRequirement('_format', 'json');
$collection
->add('entity.entity_test.canonical.json', $route);
}
}
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER][] = [
'beforePageManagerRoutes',
-155,
];
$events[RoutingEvents::ALTER][] = [
'afterPageManagerRoutes',
-165,
];
return $events;
}
}