class EntityUuidConverter

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter
  2. 10 core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter
  3. 11.x core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php \Drupal\jsonapi\ParamConverter\EntityUuidConverter

Parameter converter for upcasting entity UUIDs to full objects.

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

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

Hierarchy

Expanded class hierarchy of EntityUuidConverter

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

\Drupal\Core\ParamConverter\EntityConverter

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

File

core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php, line 27

Namespace

Drupal\jsonapi\ParamConverter
View source
class EntityUuidConverter extends EntityConverter {
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * Injects the language manager.
     *
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager to get the current content language.
     */
    public function setLanguageManager(LanguageManagerInterface $language_manager) {
        $this->languageManager = $language_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public function convert($value, $definition, $name, array $defaults) {
        $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
        $uuid_key = $this->entityTypeManager
            ->getDefinition($entity_type_id)
            ->getKey('uuid');
        if ($storage = $this->entityTypeManager
            ->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 = $this->entityRepository
                    ->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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function applies($definition, $name, Route $route) {
        return (bool) Routes::getResourceTypeNameFromParameters($route->getDefaults()) && !empty($definition['type']) && strpos($definition['type'], 'entity') === 0;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DynamicEntityTypeParamConverterTrait::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityConverter::$entityRepository protected property Entity repository.
EntityConverter::$entityTypeManager protected property Entity type manager which performs the upcasting in the end.
EntityConverter::__construct public function Constructs a new EntityConverter. 1
EntityUuidConverter::$languageManager protected property The language manager.
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
EntityUuidConverter::setLanguageManager public function Injects the language manager.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.