From d7cc9ed82cca310cfc6794397e7a61bb576a47d3 Mon Sep 17 00:00:00 2001 From: MAVRI Matina Date: Thu, 6 Jun 2024 15:30:23 +0200 Subject: [PATCH] drupal-graphql#1338: Fix error while trying to load an entity when there is no translation for a given language --- src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php | 2 +- src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php | 2 +- src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php | 2 +- src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php | 3 +++ src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php | 2 +- src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php index 408af0afd..91f1ffff6 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php @@ -171,7 +171,7 @@ 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) { + if (isset($language) && $language !== $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->hasTranslation($language)) { $entity = $entity->getTranslation($language); $entity->addCacheContexts(["static:language:{$language}"]); } diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php index a400da191..e86289fa0 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php @@ -164,7 +164,7 @@ public function resolve($type, $uuid, ?string $language, ?array $bundles, ?bool } // Get the correct translation. - if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) { + if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->hasTranslation($language)) { $entity = $entity->getTranslation($language); $entity->addCacheContexts(["static:language:{$language}"]); } diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php index 1a30c65dc..5d3f1268c 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php @@ -103,7 +103,7 @@ 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()) { + 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/Entity/EntityTranslations.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php index c78ac6b4b..f396c1a99 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php @@ -107,6 +107,9 @@ public function resolve(EntityInterface $entity, ?bool $access, ?AccountInterfac return array_map(function (LanguageInterface $language) use ($entity, $access, $accessOperation, $accessUser, $context) { $langcode = $language->getId(); + if (!$entity->hasTranslation($language)) { + return NULL; + } $entity = $entity->getTranslation($langcode); $entity->addCacheContexts(["static:language:{$langcode}"]); if ($access) { diff --git a/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php b/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php index e08c7bddd..40b871f70 100644 --- a/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php +++ b/src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php @@ -125,7 +125,7 @@ public function resolve($url, ?string $language, FieldContext $context): ?Deferr } // Get the correct translation. - if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) { + if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->hasTranslation($language)) { $entity = $entity->getTranslation($language); $entity->addCacheContexts(["static:language:{$language}"]); } diff --git a/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php b/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php index 699abbc3a..44efa655f 100644 --- a/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php +++ b/src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php @@ -174,7 +174,7 @@ public function resolve(string $vid, int $parent, ?int $max_depth, ?string $lang foreach ($entities as $id => $entity) { $context->addCacheableDependency($entities[$id]); - if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface) { + if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface && $entities[$id]->hasTranslation($language)) { $entities[$id] = $entities[$id]->getTranslation($language); $entities[$id]->addCacheContexts(["static:language:{$language}"]); }