You are here

public function EntityUuidConverter::convert in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter::convert()

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

Overrides EntityConverter::convert

File

src/ParamConverter/EntityUuidConverter.php, line 25

Class

EntityUuidConverter
Parameter converter for upcasting entity UUIDs to full objects.

Namespace

Drupal\jsonapi\ParamConverter

Code

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 the entity type is translatable, ensure we return the proper
    // translation object for the current context.
    if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
      $entity = $this->entityManager
        ->getTranslationFromContext($entity, NULL, [
        'operation' => 'entity_upcast',
      ]);
    }
    return $entity;
  }
  return NULL;
}