Completed
Pull Request — 8.x-3.x (#550)
by Philipp
10:48
created

LanguageNegotiationGraphQL::unsetCurrentLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
   * Set the current context language.
34
   *
35
   * @param string $langcode
36
   *   The language to be set.
37
   */
38
  public static function setCurrentLanguage($langcode) {
39
    \Drupal::languageManager()->reset();
40
    static::$currentLangcode = $langcode;
41
  }
42
43
  /**
44
   * Unset the current language.
45
   */
46
  public static function unsetCurrentLanguage() {
47
    static::$currentLangcode = NULL;
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function getLangcode(Request $request = NULL) {
54
    return static::$currentLangcode ?: $this->languageManager->getDefaultLanguage()->getId();
55
  }
56
57
}
58