You are here

public function EntityConverter::convert in Drupal 8

Same name and namespace 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()

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

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;
}