RouteSubscriber.php in Entity Clone 8
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\entity_clone\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
->getEntityCloneRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.clone_form", $route);
}
}
}
protected function getEntityCloneRoute(EntityTypeInterface $entity_type) {
if ($clone_form = $entity_type
->getLinkTemplate('clone-form')) {
$entity_type_id = $entity_type
->id();
$route = new Route($clone_form);
$route
->addDefaults([
'_form' => '\\Drupal\\entity_clone\\Form\\EntityCloneForm',
'_title' => 'Clone ' . $entity_type
->getLabel(),
])
->addRequirements([
'_entity_access' => $entity_type_id . '.clone',
])
->setOption('_entity_clone_entity_type_id', $entity_type_id)
->setOption('_admin_route', TRUE)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
if ($entity_type_id === 'menu_link_content') {
$route
->setRequirements([
'_permission' => 'clone ' . $entity_type_id . ' entity',
]);
}
return $route;
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = 'onAlterRoutes';
return $events;
}
}