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\ParamConverter
Code
public function convert($value, $definition, $name, array $defaults) {
$entity_type_id = $this
->getEntityTypeFromDefaults($definition, $name, $defaults);
if (!empty($definition['load_latest_revision'])) {
return $this->entityRepository
->getActive($entity_type_id, $value);
}
$contexts_repository = \Drupal::service('context.repository');
$contexts = $contexts_repository
->getAvailableContexts();
$contexts[EntityRepositoryInterface::CONTEXT_ID_LEGACY_CONTEXT_OPERATION] = new Context(new ContextDefinition('string'), 'entity_upcast');
$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;
}