You are here

class ContentModerationRouteSubscriber in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/Routing/ContentModerationRouteSubscriber.php \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
  2. 9 core/modules/content_moderation/src/Routing/ContentModerationRouteSubscriber.php \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber

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.

Hierarchy

Expanded class hierarchy of ContentModerationRouteSubscriber

See also

https://www.drupal.org/node/2940575

1 file declares its use of ContentModerationRouteSubscriber
ContentModerationRouteSubscriberTest.php in core/modules/content_moderation/tests/src/Unit/ContentModerationRouteSubscriberTest.php
1 string reference to 'ContentModerationRouteSubscriber'
content_moderation.services.yml in core/modules/content_moderation/content_moderation.services.yml
core/modules/content_moderation/content_moderation.services.yml
1 service uses ContentModerationRouteSubscriber
content_moderation.route_subscriber in core/modules/content_moderation/content_moderation.services.yml
Drupal\content_moderation\Routing\ContentModerationRouteSubscriber

File

core/modules/content_moderation/src/Routing/ContentModerationRouteSubscriber.php, line 22

Namespace

Drupal\content_moderation\Routing
View source
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.
    [
      $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() : array {
    $events = parent::getSubscribedEvents();

    // This needs to run after that EntityResolverManager has set the route
    // entity type.
    $events[RoutingEvents::ALTER] = [
      'onAlterRoutes',
      -200,
    ];
    return $events;
  }

}

Members