View source
<?php
namespace Drupal\Core\Entity\Routing;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class DefaultHtmlRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
protected $entityManager;
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity.manager'));
}
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = new RouteCollection();
$entity_type_id = $entity_type
->id();
if ($edit_route = $this
->getEditFormRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.edit_form", $edit_route);
}
if ($canonical_route = $this
->getCanonicalRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.canonical", $canonical_route);
}
if ($delete_route = $this
->getDeleteFormRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.delete_form", $delete_route);
}
return $collection;
}
protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('canonical') && $entity_type
->hasViewBuilderClass()) {
$entity_type_id = $entity_type
->id();
$route = new Route($entity_type
->getLinkTemplate('canonical'));
$route
->addDefaults([
'_entity_view' => "{$entity_type_id}.full",
'_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::title',
])
->setRequirement('_entity_access', "{$entity_type_id}.view")
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
if ($this
->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route
->setRequirement($entity_type_id, '\\d+');
}
return $route;
}
}
protected function getEditFormRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('edit-form')) {
$entity_type_id = $entity_type
->id();
$route = new Route($entity_type
->getLinkTemplate('edit-form'));
$operation = 'default';
if ($entity_type
->getFormClass('edit')) {
$operation = 'edit';
}
$route
->setDefaults([
'_entity_form' => "{$entity_type_id}.{$operation}",
'_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::editTitle',
])
->setRequirement('_entity_access', "{$entity_type_id}.update")
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
if ($this
->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route
->setRequirement($entity_type_id, '\\d+');
}
return $route;
}
}
protected function getDeleteFormRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('delete-form')) {
$entity_type_id = $entity_type
->id();
$route = new Route($entity_type
->getLinkTemplate('delete-form'));
$route
->addDefaults([
'_entity_form' => "{$entity_type_id}.delete",
'_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::deleteTitle',
])
->setRequirement('_entity_access', "{$entity_type_id}.delete")
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
if ($this
->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route
->setRequirement($entity_type_id, '\\d+');
}
return $route;
}
}
protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
if (!$entity_type
->isSubclassOf(FieldableEntityInterface::class)) {
return NULL;
}
$field_storage_definitions = $this->entityManager
->getFieldStorageDefinitions($entity_type
->id());
return $field_storage_definitions[$entity_type
->getKey('id')]
->getType();
}
}