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