3 namespace Drupal\content_moderation\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
21 class EntityModerationRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
26 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
28 protected $entityFieldManager;
31 * Constructs a new DefaultHtmlRouteProvider.
33 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_manager
36 public function __construct(EntityFieldManagerInterface $entity_manager) {
37 $this->entityFieldManager = $entity_manager;
43 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
45 $container->get('entity_field.manager')
52 public function getRoutes(EntityTypeInterface $entity_type) {
53 $collection = new RouteCollection();
55 if ($moderation_route = $this->getLatestVersionRoute($entity_type)) {
56 $entity_type_id = $entity_type->id();
57 $collection->add("entity.{$entity_type_id}.latest_version", $moderation_route);
64 * Gets the moderation-form route.
66 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
69 * @return \Symfony\Component\Routing\Route|null
70 * The generated route, if available.
72 protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
73 if ($entity_type->hasLinkTemplate('latest-version') && $entity_type->hasViewBuilderClass()) {
74 $entity_type_id = $entity_type->id();
75 $route = new Route($entity_type->getLinkTemplate('latest-version'));
78 '_entity_view' => "{$entity_type_id}.full",
79 '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
81 // If the entity type is a node, unpublished content will be visible
82 // if the user has the "view all unpublished content" permission.
83 ->setRequirement('_entity_access', "{$entity_type_id}.view")
84 ->setRequirement('_content_moderation_latest_version', 'TRUE')
85 ->setOption('_content_moderation_entity_type', $entity_type_id)
86 ->setOption('parameters', [
88 'type' => 'entity:' . $entity_type_id,
89 'load_forward_revision' => 1,
93 // Entity types with serial IDs can specify this in their route
94 // requirements, improving the matching process.
95 if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
96 $route->setRequirement($entity_type_id, '\d+');
103 * Gets the type of the ID key for a given entity type.
105 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
108 * @return string|null
109 * The type of the ID key for a given entity type, or NULL if the entity
110 * type does not support fields.
112 protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
113 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
117 $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
118 return $field_storage_definitions[$entity_type->getKey('id')]->getType();