public function EntityConverter::convert in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
- 9 core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
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'])) {
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);
if (!empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity
->bundle(), $definition['bundle'], TRUE)) {
return NULL;
}
return $entity;
}