DefaultRouteSubscriber.php in Form mode manager 8
File
src/Routing/EventSubscriber/DefaultRouteSubscriber.php
View source
<?php
namespace Drupal\form_mode_manager\Routing\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Drupal\form_mode_manager\EntityRoutingMapManager;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class DefaultRouteSubscriber extends RouteSubscriberBase {
protected $entityTypeManager;
protected $formModeManager;
protected $entityRoutingMap;
public function __construct(EntityTypeManagerInterface $entity_manager, FormModeManagerInterface $form_mode_manager, EntityRoutingMapManager $plugin_routes_manager) {
$this->entityTypeManager = $entity_manager;
$this->formModeManager = $form_mode_manager;
$this->entityRoutingMap = $plugin_routes_manager;
}
protected function alterRoutes(RouteCollection $collection) {
$form_modes_definitions = $this->formModeManager
->getAllFormModesDefinitions();
foreach (array_keys($form_modes_definitions) as $entity_type_id) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$this
->enhanceDefaultRoutes($collection, $entity_type
->id());
}
}
protected function enhanceDefaultRoutes(RouteCollection $collection, $entity_type_id) {
$this
->enhanceDefaultAddPageRoutes($collection, $entity_type_id);
$this
->enhanceDefaultAddRoutes($collection, $entity_type_id);
$this
->enhanceDefaultEditRoutes($collection, $entity_type_id);
}
protected function enhanceDefaultAddPageRoutes(RouteCollection $collection, $entity_type_id) {
$entity_add_page = $this->entityRoutingMap
->createInstance($entity_type_id, [
'entityTypeId' => $entity_type_id,
])
->getOperation('add_page');
if ($entity_add_page && ($route = $collection
->get($entity_add_page))) {
$collection
->add($entity_add_page, $this
->routeEnhancer($route, $entity_type_id));
}
}
protected function enhanceDefaultAddRoutes(RouteCollection $collection, $entity_type_id) {
$entity_route_name = $this->entityRoutingMap
->createInstance($entity_type_id, [
'entityTypeId' => $entity_type_id,
])
->getOperation('add_form');
if ($route = $collection
->get($entity_route_name)) {
$collection
->add($entity_route_name, $this
->routeEnhancer($route, $entity_type_id));
}
}
protected function enhanceDefaultEditRoutes(RouteCollection $collection, $entity_type_id) {
$entity_route_name = $this->entityRoutingMap
->createInstance($entity_type_id, [
'entityTypeId' => $entity_type_id,
])
->getOperation('edit_form');
if ($route = $collection
->get($entity_route_name)) {
$collection
->add($entity_route_name, $this
->routeEnhancer($route, $entity_type_id));
}
}
public function routeEnhancer(Route $route, $entity_type_id) {
$route
->setRequirement('_permission', "use {$entity_type_id}.default form mode");
return $route;
}
}