class EntityRevisionParamConverter

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

Parameter converter for upcasting entity revision IDs to full objects.

This is useful for pages which want to show a specific revision, like "/entity_example/{entity_example}/revision/{entity_example_revision}".

In order to use it you should specify some additional options in your route:


example.route:
  path: /foo/{entity_example_revision}
  options:
    parameters:
      entity_example_revision:
        type: entity_revision:entity_example

Hierarchy

Expanded class hierarchy of EntityRevisionParamConverter

1 file declares its use of EntityRevisionParamConverter
EntityRevisionParamConverterTest.php in core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php

File

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

Namespace

Drupal\Core\ParamConverter
View source
class EntityRevisionParamConverter implements ParamConverterInterface {
    use DynamicEntityTypeParamConverterTrait;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface
     */
    protected $entityRepository;
    
    /**
     * Creates a new EntityRevisionParamConverter instance.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
     *   The entity repository.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
        $this->entityTypeManager = $entity_type_manager;
        $this->entityRepository = $entity_repository;
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function applies($definition, $name, Route $route) {
        return isset($definition['type']) && str_contains($definition['type'], 'entity_revision:');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DynamicEntityTypeParamConverterTrait::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityRevisionParamConverter::$entityRepository protected property The entity repository.
EntityRevisionParamConverter::$entityTypeManager protected property The entity type manager.
EntityRevisionParamConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides ParamConverterInterface::applies
EntityRevisionParamConverter::convert public function Converts path variables to their corresponding objects. Overrides ParamConverterInterface::convert
EntityRevisionParamConverter::__construct public function Creates a new EntityRevisionParamConverter instance.

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