class ContentModerationRouteSubscriber

Same name in other branches
  1. 8.9.x core/modules/content_moderation/src/Routing/ContentModerationRouteSubscriber.php \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
  2. 10 core/modules/content_moderation/src/Routing/ContentModerationRouteSubscriber.php \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
  3. 11.x 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() {
        $events = parent::getSubscribedEvents();
        // This needs to run after that EntityResolverManager has set the route
        // entity type.
        $events[RoutingEvents::ALTER] = [
            'onAlterRoutes',
            -200,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ContentModerationRouteSubscriber::$entityTypeManager protected property The entity type manager.
ContentModerationRouteSubscriber::$moderatedEntityTypes protected property An associative array of moderated entity types keyed by ID.
ContentModerationRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
ContentModerationRouteSubscriber::getModeratedEntityTypes protected function Returns the moderated entity types.
ContentModerationRouteSubscriber::getSubscribedEvents public static function Overrides RouteSubscriberBase::getSubscribedEvents
ContentModerationRouteSubscriber::setLatestRevisionFlag protected function Ensure revisionable entities load the latest revision on entity forms.
ContentModerationRouteSubscriber::__construct public function ContentModerationRouteSubscriber constructor.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.