Skip to content

Commit

Permalink
Reapply "Reapply "Fix redirect settings""
Browse files Browse the repository at this point in the history
This reverts commit 4a89652.
  • Loading branch information
dbosen committed May 7, 2024
1 parent 2e793f8 commit 00bf4c0
Showing 1 changed file with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}
Expand All @@ -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.
*
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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()) {
Expand Down

0 comments on commit 00bf4c0

Please sign in to comment.