1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Enums\Languages; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
6
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase; |
8
|
|
|
use Drupal\graphql\Plugin\SchemaBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generates an enumeration of numbers. |
13
|
|
|
* |
14
|
|
|
* @GraphQLEnum( |
15
|
|
|
* id = "language_id", |
16
|
|
|
* name = "LanguageId" |
17
|
|
|
* ) |
18
|
|
|
*/ |
19
|
|
|
class LanguageId extends EnumPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The language manager. |
23
|
|
|
* |
24
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $languageManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
32
|
|
|
return new static( |
33
|
|
|
$configuration, |
34
|
|
|
$pluginId, |
35
|
|
|
$pluginDefinition, |
36
|
|
|
$container->get('language_manager') |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* LanguageId constructor. |
42
|
|
|
* |
43
|
|
|
* @param array $configuration |
44
|
|
|
* The plugin configuration array. |
45
|
|
|
* @param string $pluginId |
46
|
|
|
* The plugin id. |
47
|
|
|
* @param array $pluginDefinition |
48
|
|
|
* The plugin definition array. |
49
|
|
|
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
50
|
|
|
* The language manager service. |
51
|
|
|
*/ |
52
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, LanguageManagerInterface $languageManager) { |
53
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
54
|
|
|
$this->languageManager = $languageManager; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function buildEnumValues($definition) { |
61
|
|
|
$values = parent::buildEnumValues($definition); |
62
|
|
|
|
63
|
|
|
foreach ($this->languageManager->getLanguages() as $language) { |
64
|
|
|
$name = str_replace('-', '_', $language->getId()); |
65
|
|
|
$values[strtoupper($name)] = [ |
66
|
|
|
'value' => $language->getId(), |
67
|
|
|
'description' => $language->getName(), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $values; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|