diff --git a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php index be6b3f2..3a9d417 100644 --- a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php +++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php @@ -60,26 +60,33 @@ public function __construct(EntityManagerInterface $entity_manager) { */ 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); + $storage = $this->entityManager->getStorage($entity_type_id); + $entity_definition = $this->entityManager->getDefinition($entity_type_id); + if ($storage && $definition) { + $entity = NULL; - // If the entity type is revisionable, load the latest revision. - if ($entity instanceof EntityInterface && !empty($definition['load_latest_revision']) && $entity->getEntityType()->isRevisionable()) { + // 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()) { // @todo, replace this 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') + ->condition($entity_definition->getKey('id'), $value) + ->sort($entity_definition->getKey('revision'), 'DESC') ->range(0, 1) + ->accessCheck(FALSE) ->execute(); - $revision_ids = array_keys($entity_revisions); - $latest_revision_id = array_shift($revision_ids); - if ($entity->getRevisionId() != $latest_revision_id) { + if (!empty($entity_revisions)) { + $revision_ids = array_keys($entity_revisions); + $latest_revision_id = array_shift($revision_ids); $entity = $storage->loadRevision($latest_revision_id); } } + else { + $entity = $storage->load($value); + } // If the entity type is translatable, ensure we return the proper // translation object for the current context.