function EntityRevisionParamConverter::convert

Same name in other branches
  1. 9 core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php \Drupal\Core\ParamConverter\EntityRevisionParamConverter::convert()
  2. 8.9.x core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php \Drupal\Core\ParamConverter\EntityRevisionParamConverter::convert()
  3. 10 core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php \Drupal\Core\ParamConverter\EntityRevisionParamConverter::convert()

Overrides ParamConverterInterface::convert

File

core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php, line 62

Class

EntityRevisionParamConverter
Parameter converter for upcasting entity revision IDs to full objects.

Namespace

Drupal\Core\ParamConverter

Code

public function convert($value, $definition, $name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $entity = $storage->loadRevision($value);
    // If the entity type is translatable, ensure we return the proper
    // translation object for the current context.
    if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
        $entity = $this->entityRepository
            ->getTranslationFromContext($entity, NULL, [
            'operation' => 'entity_upcast',
        ]);
    }
    return $entity;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.