RouteSubscriber.php in Automatic Entity Label 8
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\auto_entitylabel\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
->getEntityLabelRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.auto_label", $route);
}
}
}
protected function getEntityLabelRoute(EntityTypeInterface $entity_type) {
if ($route_load = $entity_type
->getLinkTemplate('auto-label')) {
$entity_type_id = $entity_type
->id();
$route = new Route($route_load);
$route
->addDefaults([
'_form' => '\\Drupal\\auto_entitylabel\\Form\\AutoEntityLabelForm',
'_title' => 'Automatic entity label',
])
->addRequirements([
'_permission' => 'administer ' . $entity_type_id . ' labels',
])
->setOption('_admin_route', TRUE)
->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;
}
}