function EntityConverter::convert
Same name in other branches
- 8.9.x core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
- 10 core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
- 11.x core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
Overrides ParamConverterInterface::convert
1 call to EntityConverter::convert()
- AdminPathConfigEntityConverter::convert in core/
lib/ Drupal/ Core/ ParamConverter/ AdminPathConfigEntityConverter.php - Converts path variables to their corresponding objects.
2 methods override EntityConverter::convert()
- AdminPathConfigEntityConverter::convert in core/
lib/ Drupal/ Core/ ParamConverter/ AdminPathConfigEntityConverter.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 117
Class
- EntityConverter
- Parameter converter for upcasting entity IDs to full objects.
Namespace
Drupal\Core\ParamConverterCode
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;
}
// 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);
if (!empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE)) {
return NULL;
}
return $entity;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.