RouteSubscriber.php in Entity Usage 8.3
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\entity_usage\Routing;
use Drupal\Core\Config\ConfigFactoryInterface;
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;
protected $config;
public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config) {
$this->entityTypeManager = $entity_manager;
$this->config = $config;
}
protected function alterRoutes(RouteCollection $collection) {
$configured_types = $this->config
->get('entity_usage.settings')
->get('local_task_enabled_entity_types') ?: [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type
->hasLinkTemplate('canonical')) {
$template = $entity_type
->getLinkTemplate('canonical');
}
elseif ($entity_type
->hasLinkTemplate('edit-form')) {
$template = $entity_type
->getLinkTemplate('edit-form');
}
if (empty($template) || !in_array($entity_type_id, $configured_types, TRUE)) {
continue;
}
$options = [
'_admin_route' => TRUE,
'_entity_usage_entity_type_id' => $entity_type_id,
'parameters' => [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
],
];
$route = new Route($template . '/usage', [
'_controller' => '\\Drupal\\entity_usage\\Controller\\LocalTaskUsageController::listUsageLocalTask',
'_title_callback' => '\\Drupal\\entity_usage\\Controller\\LocalTaskUsageController::getTitleLocalTask',
], [
'_permission' => 'access entity usage statistics',
'_custom_access' => '\\Drupal\\entity_usage\\Controller\\LocalTaskUsageController::checkAccessLocalTask',
], $options);
$collection
->add("entity.{$entity_type_id}.entity_usage", $route);
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
99,
];
return $events;
}
}