-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathRouteLoad.php
More file actions
129 lines (117 loc) · 3.64 KB
/
RouteLoad.php
File metadata and controls
129 lines (117 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Routing;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\redirect\RedirectRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns the URL of the given path.
*
* @todo Fix the type of the output context.
*
* @DataProducer(
* id = "route_load",
* name = @Translation("Load route"),
* description = @Translation("Loads a route."),
* produces = @ContextDefinition("any",
* label = @Translation("Route")
* ),
* consumes = {
* "path" = @ContextDefinition("string",
* label = @Translation("Path")
* )
* }
* )
*/
class RouteLoad extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
/**
* The path validator service.
*
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;
/**
* Optional redirect module repository.
*
* @var \Drupal\redirect\RedirectRepository|null
*/
protected $redirectRepository;
/**
* Language manager for retrieving the default langcode.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('path.validator'),
$container->get('language_manager'),
$container->get('redirect.repository', ContainerInterface::NULL_ON_INVALID_REFERENCE)
);
}
/**
* Route constructor.
*
* @param array $configuration
* The plugin configuration.
* @param string $pluginId
* The plugin id.
* @param mixed $pluginDefinition
* The plugin definition.
* @param \Drupal\Core\Path\PathValidatorInterface $pathValidator
* The path validator service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\redirect\RedirectRepository|null $redirectRepository
*
* @codeCoverageIgnore
*/
public function __construct(
array $configuration,
$pluginId,
$pluginDefinition,
PathValidatorInterface $pathValidator,
LanguageManagerInterface $languageManager,
?RedirectRepository $redirectRepository = NULL
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->pathValidator = $pathValidator;
$this->languageManager = $languageManager;
$this->redirectRepository = $redirectRepository;
}
/**
* Resolver.
*
* @param string $path
* @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
*
* @return \Drupal\Core\Url|null
*/
public function resolve($path, RefinableCacheableDependencyInterface $metadata) {
$langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId();
$redirect = $this->redirectRepository ? $this->redirectRepository->findMatchingRedirect($path, [], $langcode) : NULL;
if ($redirect !== NULL) {
$url = $redirect->getRedirectUrl();
}
else {
$url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
}
if ($url && $url->isRouted() && $url->access()) {
return $url;
}
$metadata->addCacheTags(['4xx-response']);
return NULL;
}
}