diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderRedirect.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderRedirect.php index 7eb44edf2..3a32edbc9 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderRedirect.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderRedirect.php @@ -3,6 +3,7 @@ namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer; use Drupal\Core\Cache\RefinableCacheableDependencyInterface; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Path\PathValidatorInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -48,6 +49,7 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('language_manager'), $container->get('path.validator'), $container->get('renderer'), + $container->get('config.factory'), $container->get('redirect.repository', ContainerInterface::NULL_ON_INVALID_REFERENCE), ); } @@ -67,6 +69,8 @@ public static function create(ContainerInterface $container, array $configuratio * The path validator. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer service. + * @param \Drupal\Core\Config\ConfigFactory $config + * The config. * @param \Drupal\redirect\RedirectRepository|null $redirectRepository * The redirect repository. * @@ -79,6 +83,7 @@ public function __construct( protected LanguageManagerInterface $languageManager, protected PathValidatorInterface $pathValidator, protected RendererInterface $renderer, + protected ConfigFactory $config, protected ?RedirectRepository $redirectRepository = NULL, ) { parent::__construct($configuration, $pluginId, $pluginDefinition); @@ -99,6 +104,9 @@ public function resolve(string $path, RefinableCacheableDependencyInterface $met $metadata->addCacheTags(['redirect_list']); if ($this->redirectRepository) { + $redirectConfig = $this->config->get('redirect.settings'); + $metadata->addCacheTags($redirectConfig->getCacheTags()); + $queryString = parse_url($path, PHP_URL_QUERY) ?: ''; $pathWithoutQuery = parse_url($path, PHP_URL_PATH) ?: $path; @@ -124,26 +132,30 @@ public function resolve(string $path, RefinableCacheableDependencyInterface $met 'status' => $redirect->getStatusCode(), ]; } - } - // Ensure the path starts with a slash, fromUserInput fails otherwise. - if (!str_starts_with($path, '/')) { - $aliasPath = '/' . $path; - } - else { - $aliasPath = $path; - } - - $context = new RenderContext(); - $alias = $this->renderer->executeInRenderContext($context, function () use ($aliasPath): string { - return Url::fromUserInput($aliasPath)->toString(); - }); - - if ($alias !== $path) { - return [ - 'url' => $alias, - 'status' => 301, - ]; + if ($redirectConfig->get('route_normalizer_enabled')) { + // Ensure the path starts with a slash, fromUserInput fails otherwise. + if (!str_starts_with($path, '/')) { + $aliasPath = '/' . $path; + } + else { + $aliasPath = $path; + } + $context = new RenderContext(); + $alias = $this->renderer->executeInRenderContext($context, function () use ($aliasPath): string { + return Url::fromUserInput($aliasPath)->toString(); + }); + if (!$context->isEmpty()) { + $metadata->addCacheableDependency($context->pop()); + } + + if ($alias !== $path) { + return [ + 'url' => $alias, + 'status' => $redirectConfig->get('default_status_code'), + ]; + } + } } if (($url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path)) && $url->isRouted()) {