function EntityUuidConverter::convert
Same name in other branches
- 9 core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter::convert()
- 10 core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter::convert()
- 11.x core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter::convert()
Overrides EntityConverter::convert
File
-
core/
modules/ jsonapi/ src/ ParamConverter/ EntityUuidConverter.php, line 49
Class
- EntityUuidConverter
- Parameter converter for upcasting entity UUIDs to full objects.
Namespace
Drupal\jsonapi\ParamConverterCode
public function convert($value, $definition, $name, array $defaults) {
$entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
// @see https://www.drupal.org/project/drupal/issues/2624770
$entity_type_manager = isset($this->entityTypeManager) ? $this->entityTypeManager : $this->entityManager;
$uuid_key = $entity_type_manager->getDefinition($entity_type_id)
->getKey('uuid');
if ($storage = $entity_type_manager->getStorage($entity_type_id)) {
if (!($entities = $storage->loadByProperties([
$uuid_key => $value,
]))) {
return NULL;
}
$entity = reset($entities);
// If the entity type is translatable, ensure we return the proper
// translation object for the current context.
if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
// @see https://www.drupal.org/project/drupal/issues/2624770
$entity_repository = isset($this->entityRepository) ? $this->entityRepository : $this->entityManager;
$entity = $entity_repository->getTranslationFromContext($entity, NULL, [
'operation' => 'entity_upcast',
]);
// JSON:API always has only one method per route.
$method = $defaults[RouteObjectInterface::ROUTE_OBJECT]->getMethods()[0];
if (in_array($method, [
'PATCH',
'DELETE',
], TRUE)) {
$current_content_language = $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
if ($method === 'DELETE' && (!$entity->isDefaultTranslation() || $entity->language()
->getId() !== $current_content_language)) {
throw new MethodNotAllowedHttpException([
'GET',
], 'Deleting a resource object translation is not yet supported. See https://www.drupal.org/docs/8/modules/jsonapi/translations.');
}
if ($method === 'PATCH' && $entity->language()
->getId() !== $current_content_language) {
$available_translations = implode(', ', array_keys($entity->getTranslationLanguages()));
throw new MethodNotAllowedHttpException([
'GET',
], sprintf('The requested translation of the resource object does not exist, instead modify one of the translations that do exist: %s.', $available_translations));
}
}
}
return $entity;
}
return NULL;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.