Mercurial > hg > isophonics-drupal-site
comparison core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php @ 18:af1871eacc83
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:33:08 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
17:129ea1e6d783 | 18:af1871eacc83 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\jsonapi\ParamConverter; | |
4 | |
5 use Drupal\Core\Entity\EntityInterface; | |
6 use Drupal\Core\Language\LanguageInterface; | |
7 use Drupal\Core\Language\LanguageManagerInterface; | |
8 use Drupal\Core\ParamConverter\EntityConverter; | |
9 use Drupal\Core\TypedData\TranslatableInterface; | |
10 use Drupal\jsonapi\Routing\Routes; | |
11 use Symfony\Cmf\Component\Routing\RouteObjectInterface; | |
12 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; | |
13 use Symfony\Component\Routing\Route; | |
14 | |
15 /** | |
16 * Parameter converter for upcasting entity UUIDs to full objects. | |
17 * | |
18 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This | |
19 * class may change at any time and this will break any dependencies on it. | |
20 * | |
21 * @see https://www.drupal.org/project/jsonapi/issues/3032787 | |
22 * @see jsonapi.api.php | |
23 * | |
24 * @see \Drupal\Core\ParamConverter\EntityConverter | |
25 * | |
26 * @todo Remove when https://www.drupal.org/node/2353611 lands. | |
27 */ | |
28 class EntityUuidConverter extends EntityConverter { | |
29 | |
30 /** | |
31 * The language manager. | |
32 * | |
33 * @var \Drupal\Core\Language\LanguageManagerInterface | |
34 */ | |
35 protected $languageManager; | |
36 | |
37 /** | |
38 * Injects the language manager. | |
39 * | |
40 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager | |
41 * The language manager to get the current content language. | |
42 */ | |
43 public function setLanguageManager(LanguageManagerInterface $language_manager) { | |
44 $this->languageManager = $language_manager; | |
45 } | |
46 | |
47 /** | |
48 * {@inheritdoc} | |
49 */ | |
50 public function convert($value, $definition, $name, array $defaults) { | |
51 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); | |
52 // @see https://www.drupal.org/project/drupal/issues/2624770 | |
53 $entity_type_manager = isset($this->entityTypeManager) | |
54 ? $this->entityTypeManager | |
55 : $this->entityManager; | |
56 $uuid_key = $entity_type_manager->getDefinition($entity_type_id) | |
57 ->getKey('uuid'); | |
58 if ($storage = $entity_type_manager->getStorage($entity_type_id)) { | |
59 if (!$entities = $storage->loadByProperties([$uuid_key => $value])) { | |
60 return NULL; | |
61 } | |
62 $entity = reset($entities); | |
63 // If the entity type is translatable, ensure we return the proper | |
64 // translation object for the current context. | |
65 if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) { | |
66 // @see https://www.drupal.org/project/drupal/issues/2624770 | |
67 $entity_repository = isset($this->entityRepository) ? $this->entityRepository : $this->entityManager; | |
68 $entity = $entity_repository->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']); | |
69 // JSON:API always has only one method per route. | |
70 $method = $defaults[RouteObjectInterface::ROUTE_OBJECT]->getMethods()[0]; | |
71 if (in_array($method, ['PATCH', 'DELETE'], TRUE)) { | |
72 $current_content_language = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); | |
73 if ($method === 'DELETE' && (!$entity->isDefaultTranslation() || $entity->language()->getId() !== $current_content_language)) { | |
74 throw new MethodNotAllowedHttpException(['GET'], 'Deleting a resource object translation is not yet supported. See https://www.drupal.org/docs/8/modules/jsonapi/translations.'); | |
75 } | |
76 if ($method === 'PATCH' && $entity->language()->getId() !== $current_content_language) { | |
77 $available_translations = implode(', ', array_keys($entity->getTranslationLanguages())); | |
78 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)); | |
79 } | |
80 } | |
81 } | |
82 return $entity; | |
83 } | |
84 return NULL; | |
85 } | |
86 | |
87 /** | |
88 * {@inheritdoc} | |
89 */ | |
90 public function applies($definition, $name, Route $route) { | |
91 return ( | |
92 (bool) Routes::getResourceTypeNameFromParameters($route->getDefaults()) && | |
93 !empty($definition['type']) && strpos($definition['type'], 'entity') === 0 | |
94 ); | |
95 } | |
96 | |
97 } |