3 namespace Drupal\content_moderation\Entity\Routing;
5 use Drupal\Core\Entity\EntityFieldManagerInterface;
6 use Drupal\Core\Entity\EntityHandlerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\FieldableEntityInterface;
9 use Drupal\Core\Entity\Routing\EntityRouteProviderInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\Routing\Route;
12 use Symfony\Component\Routing\RouteCollection;
15 * Dynamic route provider for the Content moderation module.
17 * Provides the following routes:
18 * - The latest version tab, showing the latest revision of an entity, not the
23 class EntityModerationRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
28 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
30 protected $entityFieldManager;
33 * Constructs a new DefaultHtmlRouteProvider.
35 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_manager
38 public function __construct(EntityFieldManagerInterface $entity_manager) {
39 $this->entityFieldManager = $entity_manager;
45 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
47 $container->get('entity_field.manager')
54 public function getRoutes(EntityTypeInterface $entity_type) {
55 $collection = new RouteCollection();
57 if ($moderation_route = $this->getLatestVersionRoute($entity_type)) {
58 $entity_type_id = $entity_type->id();
59 $collection->add("entity.{$entity_type_id}.latest_version", $moderation_route);
66 * Gets the moderation-form route.
68 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
71 * @return \Symfony\Component\Routing\Route|null
72 * The generated route, if available.
74 protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
75 if ($entity_type->hasLinkTemplate('latest-version') && $entity_type->hasViewBuilderClass()) {
76 $entity_type_id = $entity_type->id();
77 $route = new Route($entity_type->getLinkTemplate('latest-version'));
80 '_entity_view' => "{$entity_type_id}.full",
81 '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
83 // If the entity type is a node, unpublished content will be visible
84 // if the user has the "view all unpublished content" permission.
85 ->setRequirement('_entity_access', "{$entity_type_id}.view")
86 ->setRequirement('_content_moderation_latest_version', 'TRUE')
87 ->setOption('_content_moderation_entity_type', $entity_type_id)
88 ->setOption('parameters', [
90 'type' => 'entity:' . $entity_type_id,
91 'load_latest_revision' => TRUE,
95 // Entity types with serial IDs can specify this in their route
96 // requirements, improving the matching process.
97 if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
98 $route->setRequirement($entity_type_id, '\d+');
105 * Gets the type of the ID key for a given entity type.
107 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
110 * @return string|null
111 * The type of the ID key for a given entity type, or NULL if the entity
112 * type does not support fields.
114 protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
115 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
119 $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
120 return $field_storage_definitions[$entity_type->getKey('id')]->getType();