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