Chris@14: entityTypeManager = $entity_type_manager; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: protected function alterRoutes(RouteCollection $collection) { Chris@14: foreach ($collection as $route) { Chris@14: $this->setLatestRevisionFlag($route); Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * Ensure revisionable entities load the latest revision on entity forms. Chris@14: * Chris@14: * @param \Symfony\Component\Routing\Route $route Chris@14: * The route object. Chris@14: */ Chris@14: protected function setLatestRevisionFlag(Route $route) { Chris@14: if (!$entity_form = $route->getDefault('_entity_form')) { Chris@14: return; Chris@14: } Chris@14: // Only set the flag on entity types which are revisionable. Chris@14: list($entity_type) = explode('.', $entity_form, 2); Chris@14: if (!isset($this->getModeratedEntityTypes()[$entity_type]) || !$this->getModeratedEntityTypes()[$entity_type]->isRevisionable()) { Chris@14: return; Chris@14: } Chris@14: $parameters = $route->getOption('parameters') ?: []; Chris@14: foreach ($parameters as &$parameter) { Chris@18: if (isset($parameter['type']) && $parameter['type'] === 'entity:' . $entity_type && !isset($parameter['load_latest_revision'])) { Chris@14: $parameter['load_latest_revision'] = TRUE; Chris@14: } Chris@14: } Chris@14: $route->setOption('parameters', $parameters); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the moderated entity types. Chris@14: * Chris@14: * @return \Drupal\Core\Entity\ContentEntityTypeInterface[] Chris@14: * An associative array of moderated entity types keyed by ID. Chris@14: */ Chris@14: protected function getModeratedEntityTypes() { Chris@14: if (!isset($this->moderatedEntityTypes)) { Chris@14: $entity_types = $this->entityTypeManager->getDefinitions(); Chris@14: /** @var \Drupal\workflows\WorkflowInterface $workflow */ Chris@14: foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) { Chris@14: /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */ Chris@14: $plugin = $workflow->getTypePlugin(); Chris@14: foreach ($plugin->getEntityTypes() as $entity_type_id) { Chris@14: $this->moderatedEntityTypes[$entity_type_id] = $entity_types[$entity_type_id]; Chris@14: } Chris@14: } Chris@14: } Chris@14: return $this->moderatedEntityTypes; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public static function getSubscribedEvents() { Chris@14: $events = parent::getSubscribedEvents(); Chris@14: // This needs to run after that EntityResolverManager has set the route Chris@14: // entity type. Chris@14: $events[RoutingEvents::ALTER] = ['onAlterRoutes', -200]; Chris@14: return $events; Chris@14: } Chris@14: Chris@14: }