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

LanguageNegotiationGraphQL::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\LanguageNegotiation;
4
5
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6
use Drupal\graphql\GraphQLLanguageContext;
7
use Drupal\language\LanguageNegotiationMethodBase;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Class for identifying language from a selected language.
13
 *
14
 * @LanguageNegotiation(
15
 *   id = Drupal\graphql\Plugin\LanguageNegotiation\LanguageNegotiationGraphQL::METHOD_ID,
16
 *   weight = -999,
17
 *   name = @Translation("GraphQL context"),
18
 *   description = @Translation("The current GraphQL language context. Only available while executing a query.")
19
 * )
20
 */
21
class LanguageNegotiationGraphQL extends LanguageNegotiationMethodBase implements ContainerFactoryPluginInterface {
22
23
  /**
24
   * The graphql language context.
25
   *
26
   * @var \Drupal\graphql\GraphQLLanguageContext
27
   */
28
  protected $languageContext;
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public static function create(
34
    ContainerInterface $container,
35
    array $configuration,
36
    $plugin_id,
37
    $plugin_definition
38
  ) {
39
    return new static($container->get('graphql.language_context'));
40
  }
41
42
  /**
43
   * LanguageNegotiationGraphQL constructor.
44
   *
45
   * @param \Drupal\graphql\GraphQLLanguageContext $languageContext
46
   *   Instance of the GraphQL language context.
47
   */
48
  public function __construct(GraphQLLanguageContext $languageContext) {
49
    $this->languageContext = $languageContext;
50
  }
51
52
  /**
53
   * The language negotiation method id.
54
   */
55
  const METHOD_ID = 'language-graphql';
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function getLangcode(Request $request = NULL) {
61
    return $this->languageContext->getCurrentLanguage();
62
  }
63
64
}
65