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\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 = "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
|
|
|
* arguments = { |
23
|
|
|
* "language" = "LanguageId" |
24
|
|
|
* } |
25
|
|
|
* ) |
26
|
|
|
*/ |
27
|
|
|
class Alias extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
28
|
|
|
use DependencySerializationTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instance of an alias manager. |
32
|
|
|
* |
33
|
|
|
* @var \Drupal\Core\Path\AliasManagerInterface |
34
|
|
|
*/ |
35
|
|
|
protected $aliasManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, AliasManagerInterface $aliasManager) { |
41
|
|
|
$this->aliasManager = $aliasManager; |
42
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
49
|
|
|
return new static($configuration, $pluginId, $pluginDefinition, $container->get('path.alias_manager')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
56
|
|
|
if ($value instanceof Url) { |
|
|
|
|
57
|
|
|
$internal = "/{$value->getInternalPath()}"; |
58
|
|
|
$language = isset($args['language']) ? $args['language'] : NULL; |
59
|
|
|
$alias = $this->aliasManager->getAliasByPath($internal, $language); |
60
|
|
|
|
61
|
|
|
// If the fetched alias is identical to the internal path, it means we do |
62
|
|
|
// not have a configured alias for this path. |
63
|
|
|
if ($internal !== $alias) { |
64
|
|
|
yield $alias; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|