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