diff --git a/core/lib/Drupal/Core/EventSubscriber/EntityRouteAlterSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/EntityRouteAlterSubscriber.php index bca8f68..e78a758 100644 --- a/core/lib/Drupal/Core/EventSubscriber/EntityRouteAlterSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/EntityRouteAlterSubscriber.php @@ -50,10 +50,30 @@ public function onRoutingRouteAlterSetType(RouteBuildEvent $event) { } /** + * Set the latest revision flag for entity forms. + * + * @param \Drupal\Core\Routing\RouteBuildEvent $event + * The event to process. + */ + public function onRoutingRouteAlterSetLatestRevision(RouteBuildEvent $event) { + foreach ($event->getRouteCollection() as $route) { + if (!$route->getDefault('_entity_form')) { + continue; + } + $parameters = $route->getOption('parameters') ?: []; + foreach ($parameters as &$parameter) { + $parameter['_load_latest_revision'] = TRUE; + } + $route->setOption('parameters', $parameters); + } + } + + /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[RoutingEvents::ALTER][] = ['onRoutingRouteAlterSetType', -150]; + $events[RoutingEvents::ALTER][] = ['onRoutingRouteAlterSetLatestRevision', -150]; return $events; } diff --git a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php index 67f6a89..831faf1 100644 --- a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php +++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php @@ -4,7 +4,9 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\RevisionableInterface; use Drupal\Core\TypedData\TranslatableInterface; +use Drupal\node\Entity\Node; use Symfony\Component\Routing\Route; /** @@ -62,6 +64,25 @@ public function convert($value, $definition, $name, array $defaults) { $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); if ($storage = $this->entityManager->getStorage($entity_type_id)) { $entity = $storage->load($value); + + // If the entity type is revisionable, load the latest revision. + if (!empty($definition['_load_latest_revision']) && $entity instanceof RevisionableInterface) { + // @todo, replace this with query with a standardised way of getting the + // latest revision in https://www.drupal.org/node/2784201. + $entity_revisions = $storage + ->getQuery() + ->allRevisions() + ->condition($entity->getEntityType()->getKey('id'), $entity->id()) + ->sort($entity->getEntityType()->getKey('revision'), 'DESC') + ->range(0, 1) + ->execute(); + $revision_ids = array_keys($entity_revisions); + $latest_revision_id = array_shift($revision_ids); + if ($entity->getRevisionId() != $latest_revision_id) { + $entity = $storage->loadRevision($latest_revision_id); + } + } + // If the entity type is translatable, ensure we return the proper // translation object for the current context. if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {