View source
<?php
namespace Drupal\devel\Routing;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_manager) {
$this->entityTypeManager = $entity_manager;
}
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($route = $this
->getEntityLoadRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.devel_load", $route);
}
if ($route = $this
->getEntityRenderRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.devel_render", $route);
}
if ($route = $this
->getEntityTypeDefinitionRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.devel_definition", $route);
}
}
}
protected function getEntityLoadRoute(EntityTypeInterface $entity_type) {
if ($devel_load = $entity_type
->getLinkTemplate('devel-load')) {
$entity_type_id = $entity_type
->id();
$route = new Route($devel_load);
$route
->addDefaults([
'_controller' => '\\Drupal\\devel\\Controller\\EntityDebugController::entityLoad',
'_title' => 'Devel Load',
])
->addRequirements([
'_permission' => 'access devel information',
])
->setOption('_admin_route', TRUE)
->setOption('_devel_entity_type_id', $entity_type_id)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
return $route;
}
}
protected function getEntityRenderRoute(EntityTypeInterface $entity_type) {
if ($devel_render = $entity_type
->getLinkTemplate('devel-render')) {
$entity_type_id = $entity_type
->id();
$route = new Route($devel_render);
$route
->addDefaults([
'_controller' => '\\Drupal\\devel\\Controller\\EntityDebugController::entityRender',
'_title' => 'Devel Render',
])
->addRequirements([
'_permission' => 'access devel information',
])
->setOption('_admin_route', TRUE)
->setOption('_devel_entity_type_id', $entity_type_id)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
return $route;
}
}
protected function getEntityTypeDefinitionRoute(EntityTypeInterface $entity_type) {
if ($devel_definition = $entity_type
->getLinkTemplate('devel-definition')) {
$entity_type_id = $entity_type
->id();
$route = new Route($devel_definition);
$route
->addDefaults([
'_controller' => '\\Drupal\\devel\\Controller\\EntityDebugController::entityTypeDefinition',
'_title' => 'Entity type definition',
])
->addRequirements([
'_permission' => 'access devel information',
])
->setOption('_admin_route', TRUE)
->setOption('_devel_entity_type_id', $entity_type_id)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
return $route;
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
100,
];
return $events;
}
}