Skip to content

Commit b8749e3

Browse files
chindrisfubhy
authored andcommitted
Set the default langcode for the translation manager and the configuration language for the language manager when executing a callable in a language context. (#809)
1 parent e282f04 commit b8749e3

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

graphql.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ services:
266266

267267
graphql.language_context:
268268
class: Drupal\graphql\GraphQLLanguageContext
269-
arguments: ['@language_manager']
269+
arguments: ['@language_manager', '@string_translation']
270270

271271
graphql.config_factory_override:
272272
class: Drupal\graphql\Config\GraphQLConfigOverrides

modules/graphql_core/src/Plugin/GraphQL/Fields/Menu/MenuByName.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Menu;
44

55
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6-
use Drupal\Core\Entity\EntityType;
76
use Drupal\Core\Entity\EntityTypeManagerInterface;
8-
use Drupal\Core\Entity\TranslatableInterface;
7+
use Drupal\Core\Language\LanguageManagerInterface;
98
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
109
use Drupal\graphql\GraphQL\Execution\ResolveContext;
1110
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
@@ -71,12 +70,6 @@ public function resolveValues($value, array $args, ResolveContext $context, Reso
7170
$entity = $this->entityTypeManager->getStorage('menu')->load($args['name']);
7271

7372
if ($entity instanceof MenuInterface) {
74-
if (isset($args['language']) && $args['language'] != $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->isTranslatable()) {
75-
if ($entity->hasTranslation($args['language'])) {
76-
$entity = $entity->getTranslation($args['language']);
77-
}
78-
}
79-
8073
yield $entity;
8174
}
8275
}

modules/graphql_core/src/Plugin/GraphQL/Fields/MenuLink/MenuLinkDescription.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* secure = true,
1616
* name = "description",
1717
* type = "String",
18-
* parents = {"MenuLink"}
18+
* parents = {"MenuLink"},
19+
* response_cache_contexts = {"languages:language_interface"}
1920
* )
2021
*/
2122
class MenuLinkDescription extends FieldPluginBase {

modules/graphql_core/src/Plugin/GraphQL/Fields/MenuLink/MenuLinkLabel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* secure = true,
1616
* name = "label",
1717
* type = "String",
18-
* parents = {"MenuLink"}
18+
* parents = {"MenuLink"},
19+
* response_cache_contexts = {"languages:language_interface"}
1920
* )
2021
*/
2122
class MenuLinkLabel extends FieldPluginBase {

src/GraphQLLanguageContext.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Drupal\graphql;
44

5+
use Drupal\Core\Language\LanguageDefault;
56
use Drupal\Core\Language\LanguageManagerInterface;
7+
use Drupal\Core\StringTranslation\TranslationManager;
68

79
/**
810
* Simple service that stores the current GraphQL language state.
@@ -35,14 +37,22 @@ class GraphQLLanguageContext {
3537
*/
3638
protected $languageManager;
3739

40+
/**
41+
* The string translation service
42+
*
43+
* @var \Drupal\Core\StringTranslation\TranslationManager
44+
*/
45+
protected $translationManager;
46+
3847
/**
3948
* GraphQLLanguageContext constructor.
4049
*
4150
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
4251
* The language manager service.
4352
*/
44-
public function __construct(LanguageManagerInterface $languageManager) {
53+
public function __construct(LanguageManagerInterface $languageManager, TranslationManager $translationManager) {
4554
$this->languageManager = $languageManager;
55+
$this->translationManager = $translationManager;
4656
$this->languageStack = new \SplStack();
4757
}
4858

@@ -77,6 +87,16 @@ public function executeInLanguageContext(callable $callable, $language) {
7787
$this->currentLanguage = $language;
7888
$this->isActive = TRUE;
7989
$this->languageManager->reset();
90+
// This is needed to be able to use the string translation with the
91+
// requested language.
92+
$this->translationManager->setDefaultLangcode($language);
93+
// Override the configuration language so that config entities (like menus)
94+
// are loaded using the proper translation.
95+
$currentConfigLanguage = $this->languageManager->getConfigOverrideLanguage();
96+
if ($currentConfigLanguage->getId() !== $language) {
97+
$configLanguage = $this->languageManager->getLanguage($language);
98+
$this->languageManager->setConfigOverrideLanguage($configLanguage);
99+
}
80100
// Extract the result array.
81101
try {
82102
return call_user_func($callable);
@@ -89,6 +109,12 @@ public function executeInLanguageContext(callable $callable, $language) {
89109
$this->currentLanguage = $this->languageStack->pop();
90110
$this->isActive = FALSE;
91111
$this->languageManager->reset();
112+
// Restore the languages for the translation and language managers.
113+
$defaultLangcode = !empty($this->currentLanguage)
114+
? $this->currentLanguage
115+
: $this->languageManager->getDefaultLanguage()->getId();
116+
$this->translationManager->setDefaultLangcode($defaultLangcode);
117+
$this->languageManager->setConfigOverrideLanguage($currentConfigLanguage);
92118
}
93119
}
94120

0 commit comments

Comments
 (0)