Skip to content

Commit ba1e9d4

Browse files
pmelabfubhy
authored andcommitted
Redirect path processing (#673)
1 parent e8254fb commit ba1e9d4

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

  • modules/graphql_core/src/Plugin/GraphQL/Fields/Routing

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

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

55
use Drupal\Core\Path\PathValidatorInterface;
6+
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
67
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
78
use Drupal\graphql\GraphQL\Cache\CacheableValue;
89
use Drupal\graphql\GraphQL\Execution\ResolveContext;
910
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
10-
use Drupal\language\LanguageNegotiator;
11-
use Drupal\Core\Language\LanguageManagerInterface;
12-
use Drupal\redirect\Entity\Redirect;
13-
use Drupal\Tests\views_ui\Functional\ReportFieldsTest;
1411
use GraphQL\Type\Definition\ResolveInfo;
1512
use Symfony\Component\DependencyInjection\ContainerInterface;
1613
use Symfony\Component\HttpFoundation\Request;
@@ -52,6 +49,16 @@ class Route extends FieldPluginBase implements ContainerFactoryPluginInterface {
5249
*/
5350
protected $languageManager;
5451

52+
/**
53+
* @var \Drupal\redirect\RedirectRepository
54+
*/
55+
protected $redirectRepository;
56+
57+
/**
58+
* @var InboundPathProcessorInterface
59+
*/
60+
protected $pathProcessor;
61+
5562
/**
5663
* {@inheritdoc}
5764
*/
@@ -61,8 +68,10 @@ public static function create(ContainerInterface $container, array $configuratio
6168
$plugin_id,
6269
$plugin_definition,
6370
$container->get('path.validator'),
64-
$container->has('language_negotiator') ? $container->get('language_negotiator') : NULL,
65-
$container->get('language_manager')
71+
$container->get('language_negotiator', ContainerInterface::NULL_ON_INVALID_REFERENCE),
72+
$container->get('language_manager'),
73+
$container->get('redirect.repository', ContainerInterface::NULL_ON_INVALID_REFERENCE),
74+
$container->get('path_processor_manager')
6675
);
6776
}
6877

@@ -81,16 +90,24 @@ public static function create(ContainerInterface $container, array $configuratio
8190
* The language negotiator.
8291
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
8392
* The language manager.
93+
* @param \Drupal\redirect\RedirectRepository $redirectRepository
94+
* The redirect repository, if redirect module is active.
95+
* @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface
96+
* An inbound path processor, to clean paths before redirect lookups.
8497
*/
8598
public function __construct(
8699
array $configuration,
87100
$pluginId,
88101
$pluginDefinition,
89102
PathValidatorInterface $pathValidator,
90103
$languageNegotiator,
91-
$languageManager
104+
$languageManager,
105+
$redirectRepository,
106+
$pathProcessor
92107
) {
93108
parent::__construct($configuration, $pluginId, $pluginDefinition);
109+
$this->redirectRepository = $redirectRepository;
110+
$this->pathProcessor = $pathProcessor;
94111
$this->pathValidator = $pathValidator;
95112
$this->languageNegotiator = $languageNegotiator;
96113
$this->languageManager = $languageManager;
@@ -128,23 +145,17 @@ protected function isLanguageAwareField() {
128145
return TRUE;
129146
}
130147

131-
/**
132-
* Get the redirect repository service.
133-
*
134-
* @return \Drupal\redirect\RedirectRepository
135-
*/
136-
protected function getRedirectRepository() {
137-
return \Drupal::service('redirect.repository');
138-
}
139-
140148
/**
141149
* {@inheritdoc}
142150
*/
143151
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
144-
if (\Drupal::moduleHandler()->moduleExists('redirect')) {
145-
$redirectRepository = $this->getRedirectRepository();
152+
if ($this->redirectRepository) {
146153
$currentLanguage = $this->languageManager->getCurrentLanguage()->getId();
147-
if ($redirect = $redirectRepository->findMatchingRedirect($args['path'], [], $currentLanguage)) {
154+
155+
$processedPath = $this->pathProcessor
156+
->processInbound($args['path'], Request::create($args['path']));
157+
158+
if ($redirect = $this->redirectRepository->findMatchingRedirect($processedPath, [], $currentLanguage)) {
148159
yield $redirect;
149160
return;
150161
}

0 commit comments

Comments
 (0)