From 5db8bb0cedb62dbd01ce43100bc2a9a36813ad02 Mon Sep 17 00:00:00 2001 From: bach Date: Mon, 19 Sep 2022 15:41:32 +0200 Subject: [PATCH] invalid translation language --- ...fix-invalid-translation-language-dev.patch | 98 +++++++++++++++++++ .../DataProducer/Entity/EntityLoad.php | 6 +- .../DataProducer/Entity/EntityLoadByUuid.php | 6 +- .../Entity/EntityLoadMultiple.php | 6 +- .../DataProducer/Entity/EntityTranslation.php | 4 +- .../DataProducer/Routing/RouteEntity.php | 6 +- .../Taxonomy/TaxonomyLoadTree.php | 6 +- 7 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 graphql-fix-invalid-translation-language-dev.patch diff --git a/graphql-fix-invalid-translation-language-dev.patch b/graphql-fix-invalid-translation-language-dev.patch new file mode 100644 index 000000000..e05d8975b --- /dev/null +++ b/graphql-fix-invalid-translation-language-dev.patch @@ -0,0 +1,98 @@ +diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php +index 867a28c..386b2b3 100644 +--- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php ++++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php +@@ -172,8 +172,10 @@ class EntityLoad extends DataProducerPluginBase implements ContainerFactoryPlugi + + // Get the correct translation. + if (isset($language) && $language !== $entity->language()->getId() && $entity instanceof TranslatableInterface) { +- $entity = $entity->getTranslation($language); +- $entity->addCacheContexts(["static:language:{$language}"]); ++ if ($entity->hasTranslation($language)) { ++ $entity = $entity->getTranslation($language); ++ $entity->addCacheContexts(["static:language:{$language}"]); ++ } + } + + // Check if the passed user (or current user if none is passed) has access +diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php +index 10e2d40..e4e6ed0 100644 +--- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php ++++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php +@@ -165,8 +165,10 @@ class EntityLoadByUuid extends DataProducerPluginBase implements ContainerFactor + + // Get the correct translation. + if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) { +- $entity = $entity->getTranslation($language); +- $entity->addCacheContexts(["static:language:{$language}"]); ++ if ($entity->hasTranslation($language)) { ++ $entity = $entity->getTranslation($language); ++ $entity->addCacheContexts(["static:language:{$language}"]); ++ } + } + + // Check if the passed user (or current user if none is passed) has access +diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php +index 2a0259c..27a19e5 100644 +--- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php ++++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php +@@ -172,8 +172,10 @@ class EntityLoadMultiple extends DataProducerPluginBase implements ContainerFact + } + + if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface) { +- $entities[$id] = $entities[$id]->getTranslation($language); +- $entities[$id]->addCacheContexts(["static:language:{$language}"]); ++ if ($entities[$id]->hasTranslation($language)) { ++ $entities[$id] = $entities[$id]->getTranslation($language); ++ $entities[$id]->addCacheContexts(["static:language:{$language}"]); ++ } + } + + if ($access) { +diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php +index 633bdc2..3773a9b 100644 +--- a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php ++++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php +@@ -101,7 +101,7 @@ class EntityTranslation extends DataProducerPluginBase implements ContainerFacto + * @return \Drupal\Core\Entity\EntityInterface|null + */ + public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation) { +- if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) { ++ if ($entity instanceof TranslatableInterface && $entity->isTranslatable() && $entity->hasTranslation($language)) { + $entity = $entity->getTranslation($language); + $entity->addCacheContexts(["static:language:{$language}"]); + // Check if the passed user (or current user if none is passed) has access +diff --git a/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php b/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php +index 10ea4e6..1eef22a 100644 +--- a/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php ++++ b/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php +@@ -126,8 +126,10 @@ class RouteEntity extends DataProducerPluginBase implements ContainerFactoryPlug + + // Get the correct translation. + if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) { +- $entity = $entity->getTranslation($language); +- $entity->addCacheContexts(["static:language:{$language}"]); ++ if ($entity->hasTranslation($language)) { ++ $entity = $entity->getTranslation($language); ++ $entity->addCacheContexts(["static:language:{$language}"]); ++ } + } + + $access = $entity->access('view', NULL, TRUE); +diff --git a/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php b/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php +index 1bcd624..8b2caf2 100644 +--- a/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php ++++ b/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php +@@ -175,8 +175,10 @@ class TaxonomyLoadTree extends DataProducerPluginBase implements ContainerFactor + $context->addCacheableDependency($entities[$id]); + + if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface) { +- $entities[$id] = $entities[$id]->getTranslation($language); +- $entities[$id]->addCacheContexts(["static:language:{$language}"]); ++ if ($entities[$id]->hasTranslation($language)) { ++ $entities[$id] = $entities[$id]->getTranslation($language); ++ $entities[$id]->addCacheContexts(["static:language:{$language}"]); ++ } + } + + if ($access) { diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php index 408af0afd..b6f9d8d85 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php @@ -172,8 +172,10 @@ public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $a // Get the correct translation. if (isset($language) && $language !== $entity->language()->getId() && $entity instanceof TranslatableInterface) { - $entity = $entity->getTranslation($language); - $entity->addCacheContexts(["static:language:{$language}"]); + if ($entity->hasTranslation($language)) { + $entity = $entity->getTranslation($language); + $entity->addCacheContexts(["static:language:{$language}"]); + } } // Check if the passed user (or current user if none is passed) has access diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php index a400da191..4fb1d8d74 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php @@ -165,8 +165,10 @@ public function resolve($type, $uuid, ?string $language, ?array $bundles, ?bool // Get the correct translation. if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) { - $entity = $entity->getTranslation($language); - $entity->addCacheContexts(["static:language:{$language}"]); + if ($entity->hasTranslation($language)) { + $entity = $entity->getTranslation($language); + $entity->addCacheContexts(["static:language:{$language}"]); + } } // Check if the passed user (or current user if none is passed) has access diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php index 2b456aadf..92f2c898d 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php @@ -172,8 +172,10 @@ public function resolve($type, array $ids, ?string $language, ?array $bundles, b } if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface) { - $entities[$id] = $entities[$id]->getTranslation($language); - $entities[$id]->addCacheContexts(["static:language:{$language}"]); + if ($entities[$id]->hasTranslation($language)) { + $entities[$id] = $entities[$id]->getTranslation($language); + $entities[$id]->addCacheContexts(["static:language:{$language}"]); + } } if ($access) { diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php index 1a30c65dc..0b2b6c451 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php @@ -102,8 +102,8 @@ public function __construct(array $configuration, $pluginId, $pluginDefinition, * * @return \Drupal\Core\Entity\EntityInterface|null */ - public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) { - if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) { + public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation) { + if ($entity instanceof TranslatableInterface && $entity->isTranslatable() && $entity->hasTranslation($language)) { $entity = $entity->getTranslation($language); $entity->addCacheContexts(["static:language:{$language}"]); // Check if the passed user (or current user if none is passed) has access diff --git a/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php b/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php index e08c7bddd..46c64bbb8 100644 --- a/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php +++ b/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php @@ -126,8 +126,10 @@ public function resolve($url, ?string $language, FieldContext $context): ?Deferr // Get the correct translation. if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) { - $entity = $entity->getTranslation($language); - $entity->addCacheContexts(["static:language:{$language}"]); + if ($entity->hasTranslation($language)) { + $entity = $entity->getTranslation($language); + $entity->addCacheContexts(["static:language:{$language}"]); + } } $access = $entity->access('view', NULL, TRUE); diff --git a/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php b/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php index 3f2229fd7..b0753acfa 100644 --- a/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php +++ b/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php @@ -175,8 +175,10 @@ public function resolve(string $vid, int $parent, ?int $max_depth, ?string $lang $context->addCacheableDependency($entities[$id]); if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface) { - $entities[$id] = $entities[$id]->getTranslation($language); - $entities[$id]->addCacheContexts(["static:language:{$language}"]); + if ($entities[$id]->hasTranslation($language)) { + $entities[$id] = $entities[$id]->getTranslation($language); + $entities[$id]->addCacheContexts(["static:language:{$language}"]); + } } if ($access) {