Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\content_moderation\Routing;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@14
|
6 use Drupal\Core\Routing\RouteSubscriberBase;
|
Chris@14
|
7 use Drupal\Core\Routing\RoutingEvents;
|
Chris@14
|
8 use Drupal\workflows\Entity\Workflow;
|
Chris@14
|
9 use Symfony\Component\Routing\Route;
|
Chris@14
|
10 use Symfony\Component\Routing\RouteCollection;
|
Chris@14
|
11
|
Chris@14
|
12 /**
|
Chris@14
|
13 * Subscriber for moderated revisionable entity forms.
|
Chris@14
|
14 *
|
Chris@14
|
15 * @internal
|
Chris@14
|
16 * There is ongoing discussion about how pending revisions should behave.
|
Chris@14
|
17 * The logic enabling pending revision support is likely to change once a
|
Chris@14
|
18 * decision is made.
|
Chris@14
|
19 *
|
Chris@14
|
20 * @see https://www.drupal.org/node/2940575
|
Chris@14
|
21 */
|
Chris@14
|
22 class ContentModerationRouteSubscriber extends RouteSubscriberBase {
|
Chris@14
|
23
|
Chris@14
|
24 /**
|
Chris@14
|
25 * The entity type manager.
|
Chris@14
|
26 *
|
Chris@14
|
27 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@14
|
28 */
|
Chris@14
|
29 protected $entityTypeManager;
|
Chris@14
|
30
|
Chris@14
|
31 /**
|
Chris@14
|
32 * An associative array of moderated entity types keyed by ID.
|
Chris@14
|
33 *
|
Chris@14
|
34 * @var \Drupal\Core\Entity\ContentEntityTypeInterface[]
|
Chris@14
|
35 */
|
Chris@14
|
36 protected $moderatedEntityTypes;
|
Chris@14
|
37
|
Chris@14
|
38 /**
|
Chris@14
|
39 * ContentModerationRouteSubscriber constructor.
|
Chris@14
|
40 *
|
Chris@14
|
41 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@14
|
42 * The entity type manager.
|
Chris@14
|
43 */
|
Chris@14
|
44 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
|
Chris@14
|
45 $this->entityTypeManager = $entity_type_manager;
|
Chris@14
|
46 }
|
Chris@14
|
47
|
Chris@14
|
48 /**
|
Chris@14
|
49 * {@inheritdoc}
|
Chris@14
|
50 */
|
Chris@14
|
51 protected function alterRoutes(RouteCollection $collection) {
|
Chris@14
|
52 foreach ($collection as $route) {
|
Chris@14
|
53 $this->setLatestRevisionFlag($route);
|
Chris@14
|
54 }
|
Chris@14
|
55 }
|
Chris@14
|
56
|
Chris@14
|
57 /**
|
Chris@14
|
58 * Ensure revisionable entities load the latest revision on entity forms.
|
Chris@14
|
59 *
|
Chris@14
|
60 * @param \Symfony\Component\Routing\Route $route
|
Chris@14
|
61 * The route object.
|
Chris@14
|
62 */
|
Chris@14
|
63 protected function setLatestRevisionFlag(Route $route) {
|
Chris@14
|
64 if (!$entity_form = $route->getDefault('_entity_form')) {
|
Chris@14
|
65 return;
|
Chris@14
|
66 }
|
Chris@14
|
67 // Only set the flag on entity types which are revisionable.
|
Chris@14
|
68 list($entity_type) = explode('.', $entity_form, 2);
|
Chris@14
|
69 if (!isset($this->getModeratedEntityTypes()[$entity_type]) || !$this->getModeratedEntityTypes()[$entity_type]->isRevisionable()) {
|
Chris@14
|
70 return;
|
Chris@14
|
71 }
|
Chris@14
|
72 $parameters = $route->getOption('parameters') ?: [];
|
Chris@14
|
73 foreach ($parameters as &$parameter) {
|
Chris@18
|
74 if (isset($parameter['type']) && $parameter['type'] === 'entity:' . $entity_type && !isset($parameter['load_latest_revision'])) {
|
Chris@14
|
75 $parameter['load_latest_revision'] = TRUE;
|
Chris@14
|
76 }
|
Chris@14
|
77 }
|
Chris@14
|
78 $route->setOption('parameters', $parameters);
|
Chris@14
|
79 }
|
Chris@14
|
80
|
Chris@14
|
81 /**
|
Chris@14
|
82 * Returns the moderated entity types.
|
Chris@14
|
83 *
|
Chris@14
|
84 * @return \Drupal\Core\Entity\ContentEntityTypeInterface[]
|
Chris@14
|
85 * An associative array of moderated entity types keyed by ID.
|
Chris@14
|
86 */
|
Chris@14
|
87 protected function getModeratedEntityTypes() {
|
Chris@14
|
88 if (!isset($this->moderatedEntityTypes)) {
|
Chris@14
|
89 $entity_types = $this->entityTypeManager->getDefinitions();
|
Chris@14
|
90 /** @var \Drupal\workflows\WorkflowInterface $workflow */
|
Chris@14
|
91 foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
|
Chris@14
|
92 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
|
Chris@14
|
93 $plugin = $workflow->getTypePlugin();
|
Chris@14
|
94 foreach ($plugin->getEntityTypes() as $entity_type_id) {
|
Chris@14
|
95 $this->moderatedEntityTypes[$entity_type_id] = $entity_types[$entity_type_id];
|
Chris@14
|
96 }
|
Chris@14
|
97 }
|
Chris@14
|
98 }
|
Chris@14
|
99 return $this->moderatedEntityTypes;
|
Chris@14
|
100 }
|
Chris@14
|
101
|
Chris@14
|
102 /**
|
Chris@14
|
103 * {@inheritdoc}
|
Chris@14
|
104 */
|
Chris@14
|
105 public static function getSubscribedEvents() {
|
Chris@14
|
106 $events = parent::getSubscribedEvents();
|
Chris@14
|
107 // This needs to run after that EntityResolverManager has set the route
|
Chris@14
|
108 // entity type.
|
Chris@14
|
109 $events[RoutingEvents::ALTER] = ['onAlterRoutes', -200];
|
Chris@14
|
110 return $events;
|
Chris@14
|
111 }
|
Chris@14
|
112
|
Chris@14
|
113 }
|