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\GraphQL\Cache\CacheableValue; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @GraphQLField( |
16
|
|
|
* id = "url_path", |
17
|
|
|
* secure = true, |
18
|
|
|
* name = "path", |
19
|
|
|
* description = @Translation("The processed url path."), |
20
|
|
|
* type = "String", |
21
|
|
|
* parents = {"Url"}, |
22
|
|
|
* arguments = { |
23
|
|
|
* "language" = "LanguageId" |
24
|
|
|
* } |
25
|
|
|
* ) |
26
|
|
|
*/ |
27
|
|
|
class Path extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
28
|
|
|
use DependencySerializationTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The language manager service. |
32
|
|
|
* |
33
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
34
|
|
|
*/ |
35
|
|
|
protected $languageManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Path constructor. |
39
|
|
|
* |
40
|
|
|
* @param array $configuration |
41
|
|
|
* The plugin configuration array. |
42
|
|
|
* @param string $pluginId |
43
|
|
|
* The plugin id. |
44
|
|
|
* @param mixed $pluginDefinition |
45
|
|
|
* The plugin definition. |
46
|
|
|
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
47
|
|
|
* The language manager service. |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
array $configuration, |
51
|
|
|
$pluginId, |
52
|
|
|
$pluginDefinition, |
53
|
|
|
LanguageManagerInterface $languageManager |
54
|
|
|
) { |
55
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
56
|
|
|
$this->languageManager = $languageManager; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
63
|
|
|
return new static( |
64
|
|
|
$configuration, |
65
|
|
|
$pluginId, |
66
|
|
|
$pluginDefinition, |
67
|
|
|
$container->get('language_manager') |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
75
|
|
|
if ($value instanceof Url) { |
|
|
|
|
76
|
|
|
$language = isset($args['language']) ? $this->languageManager->getLanguage($args['language']) : NULL; |
77
|
|
|
if (!empty($language)) { |
78
|
|
|
// Copy the url object and set the language. |
79
|
|
|
$value = (clone $value)->setOption('language', $language); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$url = $value->toString(TRUE); |
83
|
|
|
yield new CacheableValue($url->getGeneratedUrl(), [$url]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|