Version 1
[yaffs-website] / web / core / modules / content_moderation / src / Routing / EntityModerationRouteProvider.php
1 <?php
2
3 namespace Drupal\content_moderation\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 class EntityModerationRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
22
23   /**
24    * The entity manager.
25    *
26    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
27    */
28   protected $entityFieldManager;
29
30   /**
31    * Constructs a new DefaultHtmlRouteProvider.
32    *
33    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_manager
34    *   The entity manager.
35    */
36   public function __construct(EntityFieldManagerInterface $entity_manager) {
37     $this->entityFieldManager = $entity_manager;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
44     return new static(
45       $container->get('entity_field.manager')
46     );
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getRoutes(EntityTypeInterface $entity_type) {
53     $collection = new RouteCollection();
54
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);
58     }
59
60     return $collection;
61   }
62
63   /**
64    * Gets the moderation-form route.
65    *
66    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
67    *   The entity type.
68    *
69    * @return \Symfony\Component\Routing\Route|null
70    *   The generated route, if available.
71    */
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'));
76       $route
77         ->addDefaults([
78           '_entity_view' => "{$entity_type_id}.full",
79           '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
80         ])
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', [
87           $entity_type_id => [
88             'type' => 'entity:' . $entity_type_id,
89             'load_forward_revision' => 1,
90           ],
91         ]);
92
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+');
97       }
98       return $route;
99     }
100   }
101
102   /**
103    * Gets the type of the ID key for a given entity type.
104    *
105    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
106    *   An entity type.
107    *
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.
111    */
112   protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
113     if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
114       return NULL;
115     }
116
117     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
118     return $field_storage_definitions[$entity_type->getKey('id')]->getType();
119   }
120
121 }