3 namespace Drupal\system\Plugin\Condition;
5 use Drupal\Core\Condition\ConditionPluginBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Path\AliasManagerInterface;
8 use Drupal\Core\Path\CurrentPathStack;
9 use Drupal\Core\Path\PathMatcherInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpFoundation\RequestStack;
15 * Provides a 'Request Path' condition.
18 * id = "request_path",
19 * label = @Translation("Request Path"),
22 class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginInterface {
25 * An alias manager to find the alias for the current system path.
27 * @var \Drupal\Core\Path\AliasManagerInterface
29 protected $aliasManager;
34 * @var \Drupal\Core\Path\PathMatcherInterface
36 protected $pathMatcher;
41 * @var \Symfony\Component\HttpFoundation\RequestStack
43 protected $requestStack;
48 * @var \Drupal\Core\Path\CurrentPathStack
50 protected $currentPath;
53 * Constructs a RequestPath condition plugin.
55 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
56 * An alias manager to find the alias for the current system path.
57 * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
58 * The path matcher service.
59 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
61 * @param \Drupal\Core\Path\CurrentPathStack $current_path
63 * @param array $configuration
64 * A configuration array containing information about the plugin instance.
65 * @param string $plugin_id
66 * The plugin_id for the plugin instance.
67 * @param array $plugin_definition
68 * The plugin implementation definition.
70 public function __construct(AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, RequestStack $request_stack, CurrentPathStack $current_path, array $configuration, $plugin_id, array $plugin_definition) {
71 parent::__construct($configuration, $plugin_id, $plugin_definition);
72 $this->aliasManager = $alias_manager;
73 $this->pathMatcher = $path_matcher;
74 $this->requestStack = $request_stack;
75 $this->currentPath = $current_path;
81 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
83 $container->get('path.alias_manager'),
84 $container->get('path.matcher'),
85 $container->get('request_stack'),
86 $container->get('path.current'),
95 public function defaultConfiguration() {
96 return ['pages' => ''] + parent::defaultConfiguration();
102 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
104 '#type' => 'textarea',
105 '#title' => $this->t('Pages'),
106 '#default_value' => $this->configuration['pages'],
107 '#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [
108 '%user-wildcard' => '/user/*',
109 '%front' => '<front>',
112 return parent::buildConfigurationForm($form, $form_state);
118 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
119 $this->configuration['pages'] = $form_state->getValue('pages');
120 parent::submitConfigurationForm($form, $form_state);
126 public function summary() {
127 $pages = array_map('trim', explode("\n", $this->configuration['pages']));
128 $pages = implode(', ', $pages);
129 if (!empty($this->configuration['negate'])) {
130 return $this->t('Do not return true on the following pages: @pages', ['@pages' => $pages]);
132 return $this->t('Return true on the following pages: @pages', ['@pages' => $pages]);
138 public function evaluate() {
139 // Convert path to lowercase. This allows comparison of the same path
140 // with different case. Ex: /Page, /page, /PAGE.
141 $pages = mb_strtolower($this->configuration['pages']);
146 $request = $this->requestStack->getCurrentRequest();
147 // Compare the lowercase path alias (if any) and internal path.
148 $path = $this->currentPath->getPath($request);
149 // Do not trim a trailing slash if that is the complete path.
150 $path = $path === '/' ? $path : rtrim($path, '/');
151 $path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path));
153 return $this->pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $pages));
159 public function getCacheContexts() {
160 $contexts = parent::getCacheContexts();
161 $contexts[] = 'url.path';