EntityHierarchyRouteProvider.php in Entity Reference Hierarchy 8.2
File
src/Routing/EntityHierarchyRouteProvider.php
View source
<?php
namespace Drupal\entity_hierarchy\Routing;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Routing\EntityRouteProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class EntityHierarchyRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
const ENTITY_HIERARCHY_HAS_FIELD = '_entity_hierarchy_has_field';
const ENTITY_HIERARCHY_ENTITY_TYPE = '_entity_hierarchy_entity_type';
protected $entityTypeManager;
protected $entityFieldManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_field.manager'));
}
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = new RouteCollection();
if ($entity_type
->hasLinkTemplate('entity_hierarchy_reorder')) {
$entity_type_id = $entity_type
->id();
$route = new Route($entity_type
->getLinkTemplate('latest-version'));
$route
->setPath($entity_type
->getLinkTemplate('canonical') . '/children')
->addDefaults([
'_title' => 'Reorder children',
'_entity_form' => "{$entity_type_id}.entity_hierarchy_reorder",
])
->setRequirement('_entity_access', "{$entity_type_id}.view")
->setRequirement('_permission', 'reorder entity_hierarchy children')
->setRequirement(self::ENTITY_HIERARCHY_HAS_FIELD, 'TRUE')
->setOption(self::ENTITY_HIERARCHY_ENTITY_TYPE, $entity_type_id)
->setOption('_admin_route', TRUE)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
'load_latest_revision' => FALSE,
],
]);
$collection
->add("entity.{$entity_type_id}.entity_hierarchy_reorder", $route);
}
return $collection;
}
}