class EntityConverter

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

Parameter converter for upcasting entity IDs to full objects.

This is useful in cases where the dynamic elements of the path can't be auto-determined; for example, if your path refers to multiple of the same type of entity ("example/{node1}/foo/{node2}") or if the path can act on any entity type ("example/{entity_type}/{entity}/foo").

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


example.route:
  path: foo/{example}
  options:
    parameters:
      example:
        type: entity:node

If you want to have the entity type itself dynamic in the URL you can specify it like the following:


example.route:
  path: foo/{entity_type}/{example}
  options:
    parameters:
      example:
        type: entity:{entity_type}

The conversion can be limited to certain entity bundles by specifying a parameter 'bundle' definition property as an array:


example.route:
  path: foo/{example}
  options:
    parameters:
      example:
        type: entity:node
        bundle:
          - article
          - news

In the above example, only node entities of types 'article' and 'news' are converted. For a node of a different type, such as 'page', the route will return 404 'Not found'.

If your route needs to support pending revisions, you can specify the "load_latest_revision" parameter. This will ensure that the latest revision is returned, even if it is not the default one:


example.route:
  path: foo/{example}
  options:
    parameters:
      example:
        type: entity:node
        load_latest_revision: TRUE

When dealing with translatable entities, the "load_latest_revision" flag will make this converter load the latest revision affecting the translation matching the content language for the current request. If none can be found it will fall back to the latest revision. For instance, if an entity has an English default revision (revision 1) and an Italian pending revision (revision 2), "/foo/1" will return the former, while "/it/foo/1" will return the latter.

Hierarchy

Expanded class hierarchy of EntityConverter

See also

entities_revisions_translations

2 files declare their use of EntityConverter
EntityConverterTest.php in core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
EntityUuidConverter.php in core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php

File

core/lib/Drupal/Core/ParamConverter/EntityConverter.php, line 79

Namespace

Drupal\Core\ParamConverter
View source
class EntityConverter implements ParamConverterInterface {
    use DynamicEntityTypeParamConverterTrait;
    
    /**
     * Entity type manager which performs the upcasting in the end.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Entity repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface
     */
    protected $entityRepository;
    
    /**
     * Constructs a new EntityConverter.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
     *   The entity repository.
     *
     * @see https://www.drupal.org/node/2938929
     */
    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);
        // If the entity type is revisionable and the parameter has the
        // "load_latest_revision" flag, load the active variant.
        if (!empty($definition['load_latest_revision'])) {
            $entity = $this->entityRepository
                ->getActive($entity_type_id, $value);
            if (!empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE)) {
                return NULL;
            }
            return $entity;
        }
        $contexts = [
            'operation' => 'entity_upcast',
        ];
        $entity = $this->entityRepository
            ->getCanonical($entity_type_id, $value, $contexts);
        if (!empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE)) {
            return NULL;
        }
        return $entity;
    }
    
    /**
     * {@inheritdoc}
     */
    public function applies($definition, $name, Route $route) {
        if (!empty($definition['type']) && str_starts_with($definition['type'], 'entity:')) {
            $entity_type_id = substr($definition['type'], strlen('entity:'));
            if (str_contains($definition['type'], '{')) {
                $entity_type_slug = substr($entity_type_id, 1, -1);
                return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()
                    ->getVariables(), TRUE);
            }
            return $this->entityTypeManager
                ->hasDefinition($entity_type_id);
        }
        return FALSE;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DynamicEntityTypeParamConverterTrait::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityConverter::$entityRepository protected property Entity repository.
EntityConverter::$entityTypeManager protected property Entity type manager which performs the upcasting in the end.
EntityConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides ParamConverterInterface::applies 2
EntityConverter::convert public function Converts path variables to their corresponding objects. Overrides ParamConverterInterface::convert 2
EntityConverter::__construct public function Constructs a new EntityConverter. 1

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