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; } }