summaryrefslogtreecommitdiffstats
path: root/core/modules/content_moderation/src/Routing/ContentModerationRouteSubscriber.php
blob: 934734da3caea7e9d6a09759a19f0297f97daf18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

namespace Drupal\content_moderation\Routing;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\workflows\Entity\Workflow;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for moderated revisionable entity forms.
 *
 * @internal
 *   There is ongoing discussion about how pending revisions should behave.
 *   The logic enabling pending revision support is likely to change once a
 *   decision is made.
 *
 * @see https://www.drupal.org/node/2940575
 */
class ContentModerationRouteSubscriber extends RouteSubscriberBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * An associative array of moderated entity types keyed by ID.
   *
   * @var \Drupal\Core\Entity\ContentEntityTypeInterface[]
   */
  protected $moderatedEntityTypes;

  /**
   * ContentModerationRouteSubscriber constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection as $route) {
      $this->setLatestRevisionFlag($route);
    }
  }

  /**
   * Ensure revisionable entities load the latest revision on entity forms.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route object.
   */
  protected function setLatestRevisionFlag(Route $route) {
    if (!$entity_form = $route->getDefault('_entity_form')) {
      return;
    }
    // Only set the flag on entity types which are revisionable.
    list($entity_type) = explode('.', $entity_form, 2);
    if (!isset($this->getModeratedEntityTypes()[$entity_type]) || !$this->getModeratedEntityTypes()[$entity_type]->isRevisionable()) {
      return;
    }
    $parameters = $route->getOption('parameters') ?: [];
    foreach ($parameters as &$parameter) {
      if (isset($parameter['type']) && $parameter['type'] === 'entity:' . $entity_type && !isset($parameter['load_latest_revision'])) {
        $parameter['load_latest_revision'] = TRUE;
      }
    }
    $route->setOption('parameters', $parameters);
  }

  /**
   * Returns the moderated entity types.
   *
   * @return \Drupal\Core\Entity\ContentEntityTypeInterface[]
   *   An associative array of moderated entity types keyed by ID.
   */
  protected function getModeratedEntityTypes() {
    if (!isset($this->moderatedEntityTypes)) {
      $entity_types = $this->entityTypeManager->getDefinitions();
      /** @var \Drupal\workflows\WorkflowInterface $workflow */
      foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
        /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
        $plugin = $workflow->getTypePlugin();
        foreach ($plugin->getEntityTypes() as $entity_type_id) {
          $this->moderatedEntityTypes[$entity_type_id] = $entity_types[$entity_type_id];
        }
      }
    }
    return $this->moderatedEntityTypes;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    // This needs to run after that EntityResolverManager has set the route
    // entity type.
    $events[RoutingEvents::ALTER] = ['onAlterRoutes', -200];
    return $events;
  }

}