Chris@18: languageManager = $language_manager; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function convert($value, $definition, $name, array $defaults) { Chris@18: $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); Chris@18: // @see https://www.drupal.org/project/drupal/issues/2624770 Chris@18: $entity_type_manager = isset($this->entityTypeManager) Chris@18: ? $this->entityTypeManager Chris@18: : $this->entityManager; Chris@18: $uuid_key = $entity_type_manager->getDefinition($entity_type_id) Chris@18: ->getKey('uuid'); Chris@18: if ($storage = $entity_type_manager->getStorage($entity_type_id)) { Chris@18: if (!$entities = $storage->loadByProperties([$uuid_key => $value])) { Chris@18: return NULL; Chris@18: } Chris@18: $entity = reset($entities); Chris@18: // If the entity type is translatable, ensure we return the proper Chris@18: // translation object for the current context. Chris@18: if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) { Chris@18: // @see https://www.drupal.org/project/drupal/issues/2624770 Chris@18: $entity_repository = isset($this->entityRepository) ? $this->entityRepository : $this->entityManager; Chris@18: $entity = $entity_repository->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']); Chris@18: // JSON:API always has only one method per route. Chris@18: $method = $defaults[RouteObjectInterface::ROUTE_OBJECT]->getMethods()[0]; Chris@18: if (in_array($method, ['PATCH', 'DELETE'], TRUE)) { Chris@18: $current_content_language = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); Chris@18: if ($method === 'DELETE' && (!$entity->isDefaultTranslation() || $entity->language()->getId() !== $current_content_language)) { Chris@18: throw new MethodNotAllowedHttpException(['GET'], 'Deleting a resource object translation is not yet supported. See https://www.drupal.org/docs/8/modules/jsonapi/translations.'); Chris@18: } Chris@18: if ($method === 'PATCH' && $entity->language()->getId() !== $current_content_language) { Chris@18: $available_translations = implode(', ', array_keys($entity->getTranslationLanguages())); Chris@18: 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)); Chris@18: } Chris@18: } Chris@18: } Chris@18: return $entity; Chris@18: } Chris@18: return NULL; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function applies($definition, $name, Route $route) { Chris@18: return ( Chris@18: (bool) Routes::getResourceTypeNameFromParameters($route->getDefaults()) && Chris@18: !empty($definition['type']) && strpos($definition['type'], 'entity') === 0 Chris@18: ); Chris@18: } Chris@18: Chris@18: }