1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
8
|
|
|
use Drupal\Core\Url; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @GraphQLField( |
15
|
|
|
* id = "url_translate", |
16
|
|
|
* secure = true, |
17
|
|
|
* name = "translate", |
18
|
|
|
* description = @Translation("The translated url object."), |
19
|
|
|
* type = "Url", |
20
|
|
|
* parents = {"Url"}, |
21
|
|
|
* arguments = { |
22
|
|
|
* "language" = "LanguageId!" |
23
|
|
|
* } |
24
|
|
|
* ) |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
class Translate extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
27
|
|
|
use DependencySerializationTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The language manager service. |
31
|
|
|
* |
32
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $languageManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
40
|
|
|
return new static( |
41
|
|
|
$configuration, |
42
|
|
|
$pluginId, |
43
|
|
|
$pluginDefinition, |
44
|
|
|
$container->get('language_manager') |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Alias constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $configuration |
52
|
|
|
* The plugin configuration array. |
53
|
|
|
* @param string $pluginId |
54
|
|
|
* The plugin id. |
55
|
|
|
* @param mixed $pluginDefinition |
56
|
|
|
* The plugin definition. |
57
|
|
|
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
58
|
|
|
* The language manager service. |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
array $configuration, |
62
|
|
|
$pluginId, |
63
|
|
|
$pluginDefinition, |
64
|
|
|
LanguageManagerInterface $languageManager |
65
|
|
|
) { |
66
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
67
|
|
|
$this->languageManager = $languageManager; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
74
|
|
|
if ($value instanceof Url) { |
|
|
|
|
75
|
|
|
$language = $this->languageManager->getLanguage($args['language']); |
76
|
|
|
yield (clone $value)->setOption('language', $language); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.