EntityUuidConverter.php in JSON:API 8
File
src/ParamConverter/EntityUuidConverter.php
View source
<?php
namespace Drupal\jsonapi\ParamConverter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\ParamConverter\EntityConverter;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\jsonapi\Routing\Routes;
use Symfony\Component\Routing\Route;
class EntityUuidConverter extends EntityConverter {
public function convert($value, $definition, $name, array $defaults) {
$entity_type_id = $this
->getEntityTypeFromDefaults($definition, $name, $defaults);
if ($storage = $this->entityManager
->getStorage($entity_type_id)) {
if (!($entities = $storage
->loadByProperties([
'uuid' => $value,
]))) {
return NULL;
}
$entity = reset($entities);
if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
$entity = $this->entityManager
->getTranslationFromContext($entity, NULL, [
'operation' => 'entity_upcast',
]);
}
return $entity;
}
return NULL;
}
public function applies($definition, $name, Route $route) {
return (bool) Routes::getResourceTypeNameFromParameters($route
->getDefaults()) && !empty($definition['type']) && strpos($definition['type'], 'entity') === 0;
}
}