1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\InternalUrl; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
7
|
|
|
use Drupal\Core\Path\AliasManagerInterface; |
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
9
|
|
|
use Drupal\Core\Url; |
10
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @GraphQLField( |
17
|
|
|
* id = "internal_url_path_alias", |
18
|
|
|
* secure = true, |
19
|
|
|
* name = "pathAlias", |
20
|
|
|
* description = @Translation("The url's path alias if any."), |
21
|
|
|
* type = "String", |
22
|
|
|
* parents = {"InternalUrl"}, |
23
|
|
|
* arguments = { |
24
|
|
|
* "language" = "LanguageId" |
25
|
|
|
* } |
26
|
|
|
* ) |
27
|
|
|
*/ |
28
|
|
|
class Alias extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
29
|
|
|
use DependencySerializationTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Instance of an alias manager. |
33
|
|
|
* |
34
|
|
|
* @var \Drupal\Core\Path\AliasManagerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $aliasManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The language manager service. |
40
|
|
|
* |
41
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
42
|
|
|
*/ |
43
|
|
|
protected $languageManager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Alias constructor. |
47
|
|
|
* |
48
|
|
|
* @param array $configuration |
49
|
|
|
* The plugin configuration array. |
50
|
|
|
* @param string $pluginId |
51
|
|
|
* The plugin id. |
52
|
|
|
* @param mixed $pluginDefinition |
53
|
|
|
* The plugin definition. |
54
|
|
|
* @param \Drupal\Core\Path\AliasManagerInterface $aliasManager |
55
|
|
|
* The alias manager service. |
56
|
|
|
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
57
|
|
|
* The language manager service. |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
array $configuration, |
61
|
|
|
$pluginId, |
62
|
|
|
$pluginDefinition, |
63
|
|
|
AliasManagerInterface $aliasManager, |
64
|
|
|
LanguageManagerInterface $languageManager |
65
|
|
|
) { |
66
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
67
|
|
|
$this->aliasManager = $aliasManager; |
68
|
|
|
$this->languageManager = $languageManager; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
75
|
|
|
return new static( |
76
|
|
|
$configuration, |
77
|
|
|
$pluginId, |
78
|
|
|
$pluginDefinition, |
79
|
|
|
$container->get('path.alias_manager'), |
80
|
|
|
$container->get('language_manager') |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
88
|
|
|
if ($value instanceof Url) { |
|
|
|
|
89
|
|
|
$internal = "/{$value->getInternalPath()}"; |
90
|
|
|
$language = isset($args['language']) ? $this->languageManager->getLanguage($args['language']) : NULL; |
91
|
|
|
$alias = $this->aliasManager->getAliasByPath($internal, $language); |
92
|
|
|
|
93
|
|
|
// If the fetched alias is identical to the internal path, it means we do |
94
|
|
|
// not have a configured alias for this path. |
95
|
|
|
if ($internal !== $alias) { |
96
|
|
|
yield $alias; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|