1f73624774bed40f6143f3b73568715f2e979961
[yaffs-website] / web / core / modules / system / src / Plugin / Condition / RequestPath.php
1 <?php
2
3 namespace Drupal\system\Plugin\Condition;
4
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;
13
14 /**
15  * Provides a 'Request Path' condition.
16  *
17  * @Condition(
18  *   id = "request_path",
19  *   label = @Translation("Request Path"),
20  * )
21  */
22 class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginInterface {
23
24   /**
25    * An alias manager to find the alias for the current system path.
26    *
27    * @var \Drupal\Core\Path\AliasManagerInterface
28    */
29   protected $aliasManager;
30
31   /**
32    * The path matcher.
33    *
34    * @var \Drupal\Core\Path\PathMatcherInterface
35    */
36   protected $pathMatcher;
37
38   /**
39    * The request stack.
40    *
41    * @var \Symfony\Component\HttpFoundation\RequestStack
42    */
43   protected $requestStack;
44
45   /**
46    * The current path.
47    *
48    * @var \Drupal\Core\Path\CurrentPathStack
49    */
50   protected $currentPath;
51
52   /**
53    * Constructs a RequestPath condition plugin.
54    *
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
60    *   The request stack.
61    * @param \Drupal\Core\Path\CurrentPathStack $current_path
62    *   The 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.
69    */
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;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
82     return new static(
83       $container->get('path.alias_manager'),
84       $container->get('path.matcher'),
85       $container->get('request_stack'),
86       $container->get('path.current'),
87       $configuration,
88       $plugin_id,
89       $plugin_definition);
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function defaultConfiguration() {
96     return ['pages' => ''] + parent::defaultConfiguration();
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
103     $form['pages'] = [
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>',
110       ]),
111     ];
112     return parent::buildConfigurationForm($form, $form_state);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
119     $this->configuration['pages'] = $form_state->getValue('pages');
120     parent::submitConfigurationForm($form, $form_state);
121   }
122
123   /**
124    * {@inheritdoc}
125    */
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]);
131     }
132     return $this->t('Return true on the following pages: @pages', ['@pages' => $pages]);
133   }
134
135   /**
136    * {@inheritdoc}
137    */
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']);
142     if (!$pages) {
143       return TRUE;
144     }
145
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));
152
153     return $this->pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $pages));
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function getCacheContexts() {
160     $contexts = parent::getCacheContexts();
161     $contexts[] = 'url.path';
162     return $contexts;
163   }
164
165 }