Skip to content
Snippets Groups Projects
Commit 08ca0ded authored by Michael Strelan's avatar Michael Strelan
Browse files

Remove aliasManager from RequestPath

parent d9f9e3c7
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\path_alias\Form\SystemInformationFormAlter;
use Drupal\path_alias\Plugin\Condition\PathAliasRequestPath;
/**
* Implements hook_help().
......@@ -26,6 +27,15 @@ function path_alias_help($route_name, RouteMatchInterface $route_match): string
return '';
}
/**
* Implements hook_condition_info_alter().
*/
function path_alias_condition_info_alter(array &$definitions): void {
if (isset($definitions['request_path'])) {
$definitions['request_path']['class'] = PathAliasRequestPath::class;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
......
<?php
declare(strict_types = 1);
namespace Drupal\path_alias\Plugin\Condition;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\system\Plugin\Condition\RequestPath;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Adds path alias matching to the 'Request Path' condition.
*/
class PathAliasRequestPath extends RequestPath {
/**
* An alias manager to find the alias for the current system path.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected AliasManagerInterface $aliasManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->aliasManager = $container->get('path_alias.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public function evaluate(): bool {
// Don't bother checking path aliases if the internal path already matches.
if (parent::evaluate()) {
return TRUE;
}
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = mb_strtolower($this->configuration['pages']);
$request = $this->requestStack->getCurrentRequest();
// Compare the lowercase path alias (if any) and internal path.
$path = $this->currentPath->getPath($request);
// Do not trim a trailing slash if that is the complete path.
$path = $path === '/' ? $path : rtrim($path, '/');
$path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path));
return $this->pathMatcher->matchPath($path_alias, $pages);
}
}
......@@ -21,13 +21,6 @@
*/
class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* An alias manager to find the alias for the current system path.
*
* @var \Drupal\path_alias\AliasManagerInterface|null
*/
protected $aliasManager;
/**
* The path matcher.
*
......@@ -52,24 +45,30 @@ class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginI
/**
* Constructs a RequestPath condition plugin.
*
* @param \Drupal\path_alias\AliasManagerInterface $alias_manager
* An alias manager to find the alias for the current system path.
* @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
* @param \Drupal\Core\Path\PathMatcherInterface|\Drupal\path_alias\AliasManagerInterface $path_matcher
* The path matcher service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* @param \Symfony\Component\HttpFoundation\RequestStack|\Drupal\Core\Path\PathMatcherInterface $request_stack
* The request stack.
* @param \Drupal\Core\Path\CurrentPathStack $current_path
* @param \Drupal\Core\Path\CurrentPathStack|\Symfony\Component\HttpFoundation\RequestStack $current_path
* The current path.
* @param array $configuration
* @param array|\Drupal\Core\Path\CurrentPathStack $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* @param string|array $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* @param array|string $plugin_definition
* The plugin implementation definition.
*/
public function __construct(?AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, RequestStack $request_stack, CurrentPathStack $current_path, array $configuration, $plugin_id, array $plugin_definition) {
public function __construct(PathMatcherInterface|AliasManagerInterface $path_matcher, RequestStack|PathMatcherInterface $request_stack, CurrentPathStack|RequestStack $current_path, array|CurrentPathStack $configuration, $plugin_id, array|string $plugin_definition) {
if ($path_matcher instanceof AliasManagerInterface) {
@trigger_error('Calling ' . __CLASS__ . '::__construct() with the $alias_manager argument is deprecated in drupal:10.2.0 and is removed in drupal:11.0.0. See https://www.drupal.org/node/3096092', E_USER_DEPRECATED);
$path_matcher = func_get_arg(1);
$request_stack = func_get_arg(2);
$current_path = func_get_arg(3);
$configuration = func_get_arg(4);
$plugin_id = func_get_arg(5);
$plugin_definition = func_get_arg(6);
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->aliasManager = $alias_manager;
$this->pathMatcher = $path_matcher;
$this->requestStack = $request_stack;
$this->currentPath = $current_path;
......@@ -80,7 +79,6 @@ public function __construct(?AliasManagerInterface $alias_manager, PathMatcherIn
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->has('path_alias.manager') ? $container->get('path_alias.manager') : NULL,
$container->get('path.matcher'),
$container->get('request_stack'),
$container->get('path.current'),
......@@ -160,13 +158,11 @@ public function evaluate() {
}
$request = $this->requestStack->getCurrentRequest();
// Compare the lowercase path alias (if any) and internal path.
$path = $this->currentPath->getPath($request);
// Do not trim a trailing slash if that is the complete path.
$path = $path === '/' ? $path : rtrim($path, '/');
$path_alias = $this->aliasManager === NULL ? $path : mb_strtolower($this->aliasManager->getAliasByPath($path));
return $this->pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $pages));
return $this->pathMatcher->matchPath($path, $pages);
}
/**
......
......@@ -40,8 +40,10 @@ class RequestPathTest extends KernelTestBase {
* Modules to enable.
*
* @var array
*
* @todo split path_alias to a separate test.
*/
protected static $modules = ['system', 'user', 'field', 'path'];
protected static $modules = ['system', 'user', 'field', 'path', 'path_alias'];
/**
* The current path.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment