ac6af35d3b4ace6f6deb7a3689667c376e40df2f
[yaffs-website] / web / core / modules / content_moderation / src / Entity / Routing / EntityModerationRouteProvider.php
1 <?php
2
3 namespace Drupal\content_moderation\Entity\Routing;
4
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;
13
14 /**
15  * Dynamic route provider for the Content moderation module.
16  *
17  * Provides the following routes:
18  * - The latest version tab, showing the latest revision of an entity, not the
19  *   default one.
20  *
21  * @internal
22  */
23 class EntityModerationRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
24
25   /**
26    * The entity manager.
27    *
28    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
29    */
30   protected $entityFieldManager;
31
32   /**
33    * Constructs a new DefaultHtmlRouteProvider.
34    *
35    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_manager
36    *   The entity manager.
37    */
38   public function __construct(EntityFieldManagerInterface $entity_manager) {
39     $this->entityFieldManager = $entity_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
46     return new static(
47       $container->get('entity_field.manager')
48     );
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function getRoutes(EntityTypeInterface $entity_type) {
55     $collection = new RouteCollection();
56
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);
60     }
61
62     return $collection;
63   }
64
65   /**
66    * Gets the moderation-form route.
67    *
68    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
69    *   The entity type.
70    *
71    * @return \Symfony\Component\Routing\Route|null
72    *   The generated route, if available.
73    */
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'));
78       $route
79         ->addDefaults([
80           '_entity_view' => "{$entity_type_id}.full",
81           '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
82         ])
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', [
89           $entity_type_id => [
90             'type' => 'entity:' . $entity_type_id,
91             'load_latest_revision' => TRUE,
92           ],
93         ]);
94
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+');
99       }
100       return $route;
101     }
102   }
103
104   /**
105    * Gets the type of the ID key for a given entity type.
106    *
107    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
108    *   An entity type.
109    *
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.
113    */
114   protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
115     if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
116       return NULL;
117     }
118
119     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
120     return $field_storage_definitions[$entity_type->getKey('id')]->getType();
121   }
122
123 }