Skip to content

Commit

Permalink
drupal-graphql#1338: Fix error while trying to load an entity when th…
Browse files Browse the repository at this point in the history
…ere is no translation for a given language
  • Loading branch information
smavri-axess committed Jun 6, 2024
1 parent d9ad85d commit d7cc9ed
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]);
}
Expand Down

0 comments on commit d7cc9ed

Please sign in to comment.