function EntityConverter::convert

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

Overrides ParamConverterInterface::convert

2 calls to EntityConverter::convert()
AdminPathConfigEntityConverter::convert in core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php
Converts path variables to their corresponding objects.
EntityRevisionConverter::convert in core/modules/content_moderation/src/ParamConverter/EntityRevisionConverter.php
Converts path variables to their corresponding objects.
3 methods override EntityConverter::convert()
AdminPathConfigEntityConverter::convert in core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php
Converts path variables to their corresponding objects.
EntityRevisionConverter::convert in core/modules/content_moderation/src/ParamConverter/EntityRevisionConverter.php
Converts path variables to their corresponding objects.
EntityUuidConverter::convert in core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php
Converts path variables to their corresponding objects.

File

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

Class

EntityConverter
Parameter converter for upcasting entity 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);
    // 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'])) {
        return $this->entityRepository
            ->getActive($entity_type_id, $value);
    }
    // Do not inject the context repository as it is not an actual dependency:
    // it will be removed once both the TODOs below are fixed.
    
    /** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contexts_repository */
    $contexts_repository = \Drupal::service('context.repository');
    // @todo Consider deprecating the legacy context operation altogether in
    //   https://www.drupal.org/node/3031124.
    $contexts = $contexts_repository->getAvailableContexts();
    $contexts[EntityRepositoryInterface::CONTEXT_ID_LEGACY_CONTEXT_OPERATION] = new Context(new ContextDefinition('string'), 'entity_upcast');
    // @todo At the moment we do not need the current user context, which is
    //   triggering some test failures. We can remove these lines once
    //   https://www.drupal.org/node/2934192 is fixed.
    $context_id = '@user.current_user_context:current_user';
    if (isset($contexts[$context_id])) {
        $account = $contexts[$context_id]->getContextValue();
        unset($account->_skipProtectedUserFieldConstraint);
        unset($contexts[$context_id]);
    }
    $entity = $this->entityRepository
        ->getCanonical($entity_type_id, $value, $contexts);
    return $entity;
}

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