1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Path\PathValidatorInterface; |
6
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
7
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
8
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
10
|
|
|
use Drupal\language\LanguageNegotiator; |
11
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Retrieve a route object based on a path. |
17
|
|
|
* |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "url_route", |
20
|
|
|
* secure = true, |
21
|
|
|
* name = "route", |
22
|
|
|
* description = @Translation("Loads a route by its path."), |
23
|
|
|
* type = "Url", |
24
|
|
|
* arguments = { |
25
|
|
|
* "path" = "String!" |
26
|
|
|
* } |
27
|
|
|
* ) |
28
|
|
|
*/ |
29
|
|
|
class Route extends FieldPluginBase implements ContainerFactoryPluginInterface { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The path validator service. |
33
|
|
|
* |
34
|
|
|
* @var \Drupal\Core\Path\PathValidatorInterface |
35
|
|
|
*/ |
36
|
|
|
protected $pathValidator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The language negotiator service. |
40
|
|
|
* |
41
|
|
|
* @var \Drupal\language\LanguageNegotiator |
42
|
|
|
*/ |
43
|
|
|
protected $languageNegotiator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
49
|
|
|
return new static( |
50
|
|
|
$configuration, |
51
|
|
|
$plugin_id, |
52
|
|
|
$plugin_definition, |
53
|
|
|
$container->get('path.validator'), |
54
|
|
|
$container->has('language_negotiator') ? $container->get('language_negotiator') : NULL |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Route constructor. |
60
|
|
|
* |
61
|
|
|
* @param array $configuration |
62
|
|
|
* The plugin configuration. |
63
|
|
|
* @param string $pluginId |
64
|
|
|
* The plugin id. |
65
|
|
|
* @param mixed $pluginDefinition |
66
|
|
|
* The plugin definition. |
67
|
|
|
* @param \Drupal\Core\Path\PathValidatorInterface $pathValidator |
68
|
|
|
* The path validator service. |
69
|
|
|
* @param \Drupal\language\LanguageNegotiator|null $languageNegotiator |
70
|
|
|
* The language negotiator. |
71
|
|
|
*/ |
72
|
|
|
public function __construct( |
73
|
|
|
array $configuration, |
74
|
|
|
$pluginId, |
75
|
|
|
$pluginDefinition, |
76
|
|
|
PathValidatorInterface $pathValidator, |
77
|
|
|
$languageNegotiator |
78
|
|
|
) { |
79
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
80
|
|
|
$this->pathValidator = $pathValidator; |
81
|
|
|
$this->languageNegotiator = $languageNegotiator; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
88
|
|
|
if (($url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($args['path'])) && $url->access()) { |
89
|
|
|
|
90
|
|
|
// For now we just take the "url" negotiator into account. |
91
|
|
|
if ($this->languageNegotiator) { |
92
|
|
|
if ($negotiator = $this->languageNegotiator->getNegotiationMethodInstance('language-url')) { |
93
|
|
|
$context->setContext('language', $negotiator->getLangcode(Request::create($args['path'])), $info); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
yield $url; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
else { |
101
|
|
|
yield (new CacheableValue(NULL))->addCacheTags(['4xx-response']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|