Skip to content

Commit 6dfa31a

Browse files
authored
Use the path validator service to process the route field. (#545)
1 parent 2132807 commit 6dfa31a

3 files changed

Lines changed: 48 additions & 15 deletions

File tree

modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/Route.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing;
44

55
use Drupal\Component\Utility\UrlHelper;
6+
use Drupal\Core\Path\PathValidatorInterface;
7+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
68
use Drupal\Core\Url;
79
use Drupal\graphql\GraphQL\Cache\CacheableValue;
810
use Drupal\graphql\GraphQL\Execution\ResolveContext;
911
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
1012
use GraphQL\Type\Definition\ResolveInfo;
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
1114

1215
/**
1316
* Retrieve a route object based on a path.
@@ -23,23 +26,53 @@
2326
* }
2427
* )
2528
*/
26-
class Route extends FieldPluginBase {
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+
* {@inheritdoc}
40+
*/
41+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
42+
return new static(
43+
$configuration,
44+
$plugin_id,
45+
$plugin_definition,
46+
$container->get('path.validator')
47+
);
48+
}
49+
50+
/**
51+
* Route constructor.
52+
*
53+
* @param array $configuration
54+
* The plugin configuration.
55+
* @param string $pluginId
56+
* The plugin id.
57+
* @param mixed $pluginDefinition
58+
* The plugin definition.
59+
* @param \Drupal\Core\Path\PathValidatorInterface $pathValidator
60+
* The path validator service.
61+
*/
62+
public function __construct(array $configuration, $pluginId, $pluginDefinition, PathValidatorInterface $pathValidator) {
63+
parent::__construct($configuration, $pluginId, $pluginDefinition);
64+
$this->pathValidator = $pathValidator;
65+
}
2766

2867
/**
2968
* {@inheritdoc}
3069
*/
3170
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
32-
if (UrlHelper::isExternal($args['path'])) {
33-
yield Url::fromUri($args['path']);
71+
if (($url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($args['path'])) && $url->access()) {
72+
yield $url;
3473
}
3574
else {
36-
$url = Url::fromUri("internal:{$args['path']}", ['routed_path' => $args['path']]);
37-
if ($url->isRouted() && $url->access()) {
38-
yield $url;
39-
}
40-
else {
41-
yield (new CacheableValue(NULL))->addCacheTags(['4xx-response']);
42-
}
75+
yield (new CacheableValue(NULL))->addCacheTags(['4xx-response']);
4376
}
4477
}
4578

modules/graphql_core/tests/src/Kernel/Languages/LanguageTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function testLanguageSwitchLinks() {
122122
'id' => 'en',
123123
],
124124
'url' => [
125-
'path' => '/en',
125+
'path' => '/en/user/login',
126126
],
127127
'title' => 'English',
128128
'active' => TRUE,
@@ -132,7 +132,7 @@ public function testLanguageSwitchLinks() {
132132
'id' => 'fr',
133133
],
134134
'url' => [
135-
'path' => '/fr',
135+
'path' => '/fr/user/login',
136136
],
137137
'title' => NULL,
138138
'active' => FALSE,
@@ -142,7 +142,7 @@ public function testLanguageSwitchLinks() {
142142
'id' => 'es',
143143
],
144144
'url' => [
145-
'path' => '/es',
145+
'path' => '/es/user/login',
146146
],
147147
'title' => NULL,
148148
'active' => FALSE,
@@ -152,7 +152,7 @@ public function testLanguageSwitchLinks() {
152152
'id' => 'pt-br',
153153
],
154154
'url' => [
155-
'path' => '/',
155+
'path' => '/user/login',
156156
],
157157
'title' => NULL,
158158
'active' => FALSE,

modules/graphql_core/tests/src/Kernel/Routing/RouteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp() {
3737
->willReturn('graphql/test/a');
3838

3939
$urlGenerator
40-
->generateFromRoute('graphql_context_test.a', [], ['routed_path' => '/graphql/test/a', 'query' => []], TRUE)
40+
->generateFromRoute('graphql_context_test.a', [], ['query' => []], TRUE)
4141
->willReturn((new GeneratedUrl())->setGeneratedUrl('/my/other/alias'));
4242

4343
$this->container->set('path.alias_manager', $aliasManager->reveal());

0 commit comments

Comments
 (0)