You are here

class EntityUuidConverter in JSON:API 8

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

Parameter converter for upcasting entity UUIDs to full objects.

@todo Remove when https://www.drupal.org/node/2353611 lands.

@internal

Hierarchy

Expanded class hierarchy of EntityUuidConverter

See also

\Drupal\Core\ParamConverter\EntityConverter

1 string reference to 'EntityUuidConverter'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses EntityUuidConverter
paramconverter.jsonapi.entity_uuid in ./jsonapi.services.yml
Drupal\jsonapi\ParamConverter\EntityUuidConverter

File

src/ParamConverter/EntityUuidConverter.php, line 20

Namespace

Drupal\jsonapi\ParamConverter
View source
class EntityUuidConverter extends EntityConverter {

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    return (bool) Routes::getResourceTypeNameFromParameters($route
      ->getDefaults()) && !empty($definition['type']) && strpos($definition['type'], 'entity') === 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
DynamicEntityTypeParamConverterTrait::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityConverter::$deprecatedProperties protected property
EntityConverter::$entityRepository protected property Entity repository.
EntityConverter::$entityTypeManager protected property Entity type manager which performs the upcasting in the end.
EntityConverter::getLatestTranslationAffectedRevision Deprecated protected function Returns the latest revision translation of the specified entity.
EntityConverter::languageManager protected function Returns a language manager instance.
EntityConverter::loadRevision Deprecated protected function Loads the specified entity revision.
EntityConverter::__construct public function Constructs a new EntityConverter. 1
EntityUuidConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides EntityConverter::applies
EntityUuidConverter::convert public function Converts path variables to their corresponding objects. Overrides EntityConverter::convert