Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drupal-graphql#1338: Fix error while trying to load an entity when there is no translation for a given language #1408

Open
wants to merge 2 commits into
base: 8.x-4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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($langcode)) {
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