|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\LanguageNegotiation; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\language\LanguageNegotiationMethodBase; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class for identifying language from a selected language. |
|
10
|
|
|
* |
|
11
|
|
|
* @LanguageNegotiation( |
|
12
|
|
|
* id = Drupal\graphql\Plugin\LanguageNegotiation\LanguageNegotiationGraphQL::METHOD_ID, |
|
13
|
|
|
* weight = 12, |
|
14
|
|
|
* name = @Translation("GraphQL context"), |
|
15
|
|
|
* description = @Translation("The current GraphQL language context. Only available while executing a query.") |
|
16
|
|
|
* ) |
|
17
|
|
|
*/ |
|
18
|
|
|
class LanguageNegotiationGraphQL extends LanguageNegotiationMethodBase { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The language negotiation method id. |
|
22
|
|
|
*/ |
|
23
|
|
|
const METHOD_ID = 'language-graphql'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The current langcode. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $currentLangcode; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* State if the language context is currently active. |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $contextActive; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set the current context language. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $langcode |
|
43
|
|
|
* The language to be set. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function setCurrentLanguage($langcode) { |
|
46
|
|
|
\Drupal::languageManager()->reset(); |
|
47
|
|
|
static::$currentLangcode = $langcode; |
|
48
|
|
|
static::$contextActive = TRUE; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Unset the current language. |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function unsetCurrentLanguage() { |
|
55
|
|
|
static::$currentLangcode = NULL; |
|
56
|
|
|
static::$contextActive = FALSE; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getLangcode(Request $request = NULL) { |
|
63
|
|
|
return static::$contextActive |
|
64
|
|
|
? (static::$currentLangcode ?: \Drupal::languageManager()->getDefaultLanguage()->getId()) |
|
65
|
|
|
: NULL; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|