EntityModerationRouteProvider.php in Drupal 9
File
core/modules/content_moderation/src/Entity/Routing/EntityModerationRouteProvider.php
View source
<?php
namespace Drupal\content_moderation\Entity\Routing;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Routing\EntityRouteProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class EntityModerationRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
protected $entityFieldManager;
public function __construct(EntityFieldManagerInterface $entity_field_manager) {
$this->entityFieldManager = $entity_field_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_field.manager'));
}
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = new RouteCollection();
if ($moderation_route = $this
->getLatestVersionRoute($entity_type)) {
$entity_type_id = $entity_type
->id();
$collection
->add("entity.{$entity_type_id}.latest_version", $moderation_route);
}
return $collection;
}
protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('latest-version') && $entity_type
->hasViewBuilderClass()) {
$entity_type_id = $entity_type
->id();
$route = new Route($entity_type
->getLinkTemplate('latest-version'));
$route
->addDefaults([
'_entity_view' => "{$entity_type_id}.full",
'_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::title',
])
->setRequirement('_entity_access', "{$entity_type_id}.view")
->setRequirement('_content_moderation_latest_version', 'TRUE')
->setOption('_content_moderation_entity_type', $entity_type_id)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
'load_latest_revision' => TRUE,
],
]);
if ($this
->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route
->setRequirement($entity_type_id, '\\d+');
}
return $route;
}
}
protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
if (!$entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
return NULL;
}
$field_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type
->id());
return $field_storage_definitions[$entity_type
->getKey('id')]
->getType();
}
}