diff --git a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php index 5eec388bbd..1f6b42b609 100644 --- a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php +++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php @@ -2,6 +2,7 @@ namespace Drupal\Core\ParamConverter; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; @@ -65,15 +66,15 @@ public function convert($value, $definition, $name, array $defaults) { $storage = $this->entityManager->getStorage($entity_type_id); $entity_definition = $this->entityManager->getDefinition($entity_type_id); - $entity = NULL; + $entity = $storage->load($value); // If the entity type is revisionable and the parameter has the // "load_latest_revision" flag, load the latest revision. - if (!empty($definition['load_latest_revision']) && $entity_definition->isRevisionable()) { - $entity = $this->loadLatestRevision($storage, $entity_definition, $value); - } - else { - $entity = $storage->load($value); + if ($entity instanceof ContentEntityInterface && !empty($definition['load_latest_revision']) && $entity_definition->isRevisionable()) { + $latest_revision_id = $this->getLatestRevisionId($storage, $entity_definition, $value); + if ($entity->getLoadedRevisionId() !== $latest_revision_id) { + $entity = $storage->loadRevision($latest_revision_id); + } } // If the entity type is translatable, ensure we return the proper @@ -86,7 +87,7 @@ public function convert($value, $definition, $name, array $defaults) { } /** - * Load the latest revision of an entity. + * Get the latest revision ID. * * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The entity storage. @@ -95,10 +96,10 @@ public function convert($value, $definition, $name, array $defaults) { * @param mixed $value * The raw value. * - * @return \Drupal\Core\Entity\EntityInterface|null - * The loaded entity if it exists, NULL otherwise. + * @return int + * The latest revision ID for a given entity. */ - protected function loadLatestRevision(EntityStorageInterface $storage, EntityTypeInterface $entity_definition, $value) { + protected function getLatestRevisionId(EntityStorageInterface $storage, EntityTypeInterface $entity_definition, $value) { // @todo, replace this query with a standardized way of getting the // latest revision in https://www.drupal.org/node/2784201. $result = $storage @@ -109,8 +110,7 @@ protected function loadLatestRevision(EntityStorageInterface $storage, EntityTyp // access check when looking up the latest revision. ->accessCheck(FALSE) ->execute(); - - return !empty($result) ? $storage->loadRevision(key($result)) : NULL; + return key($result); } /**