Skip to content

Commit

Permalink
[Feature]: Disabling of language fallback (#907)
Browse files Browse the repository at this point in the history
* Added getFallbackLanguageValue argument

* Apply php-cs-fixer changes

* Added sample request

---------

Co-authored-by: mcop1 <[email protected]>
  • Loading branch information
mcop1 and mcop1 authored Oct 30, 2024
1 parent 18a4902 commit 90aabfc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions doc/10_GraphQL/04_Query/08_Localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,18 @@ However, you can always provide an alternative language for a specific field.
}
}
```

### Fallback Language

You can disable getting the value of the fallback language by passing the `getFallbackLanguageValue` argument.
Set it to `false` to disable the fallback language.

##### Sample Request
```
query {
getCar(id: 1229)
{
name(language:"de", getFallbackLanguageValue:false)
}
}
```
8 changes: 7 additions & 1 deletion src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ public function enrichConfig($fieldDefinition, $class, $attribute, $graphQLConfi
if ($container instanceof Data\Localizedfields) {
$graphQLConfig['args'] = $graphQLConfig['args'] ?? [];
$graphQLConfig['args'] = array_merge($graphQLConfig['args'],
['language' => ['type' => Type::string()],
[
'language' => [
'type' => Type::string(),
],
'getFallbackLanguageValue' => [
'type' => Type::boolean(),
],
]);
}

Expand Down
9 changes: 8 additions & 1 deletion src/GraphQL/FieldHelper/DataObjectFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,14 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex
$container,
$getter
) {
return $container->$getter($args['language'] ?? null);
$orgUseFallbackValues = Localizedfield::getGetFallbackValues();
Localizedfield::setGetFallbackValues(
$args['getFallbackLanguageValue'] ?? $orgUseFallbackValues
);
$localizedValue = $container->$getter($args['language'] ?? null);
Localizedfield::setGetFallbackValues($orgUseFallbackValues);

return $localizedValue;
};
} else {
$data[$astName] = $container->$getter();
Expand Down

0 comments on commit 90aabfc

Please sign in to comment.