3 namespace Drupal\system\Plugin\Condition;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Condition\ConditionPluginBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Path\AliasManagerInterface;
9 use Drupal\Core\Path\CurrentPathStack;
10 use Drupal\Core\Path\PathMatcherInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\HttpFoundation\RequestStack;
16 * Provides a 'Request Path' condition.
19 * id = "request_path",
20 * label = @Translation("Request Path"),
23 class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginInterface {
26 * An alias manager to find the alias for the current system path.
28 * @var \Drupal\Core\Path\AliasManagerInterface
30 protected $aliasManager;
35 * @var \Drupal\Core\Path\PathMatcherInterface
37 protected $pathMatcher;
42 * @var \Symfony\Component\HttpFoundation\RequestStack
44 protected $requestStack;
49 * @var \Drupal\Core\Path\CurrentPathStack
51 protected $currentPath;
54 * Constructs a RequestPath condition plugin.
56 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
57 * An alias manager to find the alias for the current system path.
58 * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
59 * The path matcher service.
60 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
62 * @param \Drupal\Core\Path\CurrentPathStack $current_path
64 * @param array $configuration
65 * A configuration array containing information about the plugin instance.
66 * @param string $plugin_id
67 * The plugin_id for the plugin instance.
68 * @param array $plugin_definition
69 * The plugin implementation definition.
71 public function __construct(AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, RequestStack $request_stack, CurrentPathStack $current_path, array $configuration, $plugin_id, array $plugin_definition) {
72 parent::__construct($configuration, $plugin_id, $plugin_definition);
73 $this->aliasManager = $alias_manager;
74 $this->pathMatcher = $path_matcher;
75 $this->requestStack = $request_stack;
76 $this->currentPath = $current_path;
82 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
84 $container->get('path.alias_manager'),
85 $container->get('path.matcher'),
86 $container->get('request_stack'),
87 $container->get('path.current'),
96 public function defaultConfiguration() {
97 return ['pages' => ''] + parent::defaultConfiguration();
103 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
105 '#type' => 'textarea',
106 '#title' => $this->t('Pages'),
107 '#default_value' => $this->configuration['pages'],
108 '#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.", [
109 '%user-wildcard' => '/user/*',
110 '%front' => '<front>',
113 return parent::buildConfigurationForm($form, $form_state);
119 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
120 $this->configuration['pages'] = $form_state->getValue('pages');
121 parent::submitConfigurationForm($form, $form_state);
127 public function summary() {
128 $pages = array_map('trim', explode("\n", $this->configuration['pages']));
129 $pages = implode(', ', $pages);
130 if (!empty($this->configuration['negate'])) {
131 return $this->t('Do not return true on the following pages: @pages', ['@pages' => $pages]);
133 return $this->t('Return true on the following pages: @pages', ['@pages' => $pages]);
139 public function evaluate() {
140 // Convert path to lowercase. This allows comparison of the same path
141 // with different case. Ex: /Page, /page, /PAGE.
142 $pages = Unicode::strtolower($this->configuration['pages']);
147 $request = $this->requestStack->getCurrentRequest();
148 // Compare the lowercase path alias (if any) and internal path.
149 $path = $this->currentPath->getPath($request);
150 // Do not trim a trailing slash if that is the complete path.
151 $path = $path === '/' ? $path : rtrim($path, '/');
152 $path_alias = Unicode::strtolower($this->aliasManager->getAliasByPath($path));
154 return $this->pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $pages));
160 public function getCacheContexts() {
161 $contexts = parent::getCacheContexts();
162 $contexts[] = 'url.path';