Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Block/BlockBase.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\Core\Block; | |
4 | |
5 use Drupal\Core\Access\AccessResult; | |
6 use Drupal\Core\Form\FormStateInterface; | |
7 use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait; | |
8 use Drupal\Core\Plugin\ContextAwarePluginBase; | |
9 use Drupal\Component\Utility\Unicode; | |
10 use Drupal\Component\Utility\NestedArray; | |
11 use Drupal\Core\Language\LanguageInterface; | |
12 use Drupal\Core\Plugin\PluginWithFormsInterface; | |
13 use Drupal\Core\Plugin\PluginWithFormsTrait; | |
14 use Drupal\Core\Session\AccountInterface; | |
15 use Drupal\Component\Transliteration\TransliterationInterface; | |
16 | |
17 /** | |
18 * Defines a base block implementation that most blocks plugins will extend. | |
19 * | |
20 * This abstract class provides the generic block configuration form, default | |
21 * block settings, and handling for general user-defined block visibility | |
22 * settings. | |
23 * | |
24 * @ingroup block_api | |
25 */ | |
26 abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginInterface, PluginWithFormsInterface { | |
27 | |
28 use ContextAwarePluginAssignmentTrait; | |
29 use PluginWithFormsTrait; | |
30 | |
31 /** | |
32 * The transliteration service. | |
33 * | |
34 * @var \Drupal\Component\Transliteration\TransliterationInterface | |
35 */ | |
36 protected $transliteration; | |
37 | |
38 /** | |
39 * {@inheritdoc} | |
40 */ | |
41 public function label() { | |
42 if (!empty($this->configuration['label'])) { | |
43 return $this->configuration['label']; | |
44 } | |
45 | |
46 $definition = $this->getPluginDefinition(); | |
47 // Cast the admin label to a string since it is an object. | |
48 // @see \Drupal\Core\StringTranslation\TranslatableMarkup | |
49 return (string) $definition['admin_label']; | |
50 } | |
51 | |
52 /** | |
53 * {@inheritdoc} | |
54 */ | |
55 public function __construct(array $configuration, $plugin_id, $plugin_definition) { | |
56 parent::__construct($configuration, $plugin_id, $plugin_definition); | |
57 $this->setConfiguration($configuration); | |
58 } | |
59 | |
60 /** | |
61 * {@inheritdoc} | |
62 */ | |
63 public function getConfiguration() { | |
64 return $this->configuration; | |
65 } | |
66 | |
67 /** | |
68 * {@inheritdoc} | |
69 */ | |
70 public function setConfiguration(array $configuration) { | |
71 $this->configuration = NestedArray::mergeDeep( | |
72 $this->baseConfigurationDefaults(), | |
73 $this->defaultConfiguration(), | |
74 $configuration | |
75 ); | |
76 } | |
77 | |
78 /** | |
79 * Returns generic default configuration for block plugins. | |
80 * | |
81 * @return array | |
82 * An associative array with the default configuration. | |
83 */ | |
84 protected function baseConfigurationDefaults() { | |
85 return [ | |
86 'id' => $this->getPluginId(), | |
87 'label' => '', | |
88 'provider' => $this->pluginDefinition['provider'], | |
89 'label_display' => static::BLOCK_LABEL_VISIBLE, | |
90 ]; | |
91 } | |
92 | |
93 /** | |
94 * {@inheritdoc} | |
95 */ | |
96 public function defaultConfiguration() { | |
97 return []; | |
98 } | |
99 | |
100 /** | |
101 * {@inheritdoc} | |
102 */ | |
103 public function setConfigurationValue($key, $value) { | |
104 $this->configuration[$key] = $value; | |
105 } | |
106 | |
107 /** | |
108 * {@inheritdoc} | |
109 */ | |
110 public function calculateDependencies() { | |
111 return []; | |
112 } | |
113 | |
114 /** | |
115 * {@inheritdoc} | |
116 */ | |
117 public function access(AccountInterface $account, $return_as_object = FALSE) { | |
118 $access = $this->blockAccess($account); | |
119 return $return_as_object ? $access : $access->isAllowed(); | |
120 } | |
121 | |
122 /** | |
123 * Indicates whether the block should be shown. | |
124 * | |
125 * Blocks with specific access checking should override this method rather | |
126 * than access(), in order to avoid repeating the handling of the | |
127 * $return_as_object argument. | |
128 * | |
129 * @param \Drupal\Core\Session\AccountInterface $account | |
130 * The user session for which to check access. | |
131 * | |
132 * @return \Drupal\Core\Access\AccessResult | |
133 * The access result. | |
134 * | |
135 * @see self::access() | |
136 */ | |
137 protected function blockAccess(AccountInterface $account) { | |
138 // By default, the block is visible. | |
139 return AccessResult::allowed(); | |
140 } | |
141 | |
142 /** | |
143 * {@inheritdoc} | |
144 * | |
145 * Creates a generic configuration form for all block types. Individual | |
146 * block plugins can add elements to this form by overriding | |
147 * BlockBase::blockForm(). Most block plugins should not override this | |
148 * method unless they need to alter the generic form elements. | |
149 * | |
150 * @see \Drupal\Core\Block\BlockBase::blockForm() | |
151 */ | |
152 public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
153 $definition = $this->getPluginDefinition(); | |
154 $form['provider'] = [ | |
155 '#type' => 'value', | |
156 '#value' => $definition['provider'], | |
157 ]; | |
158 | |
159 $form['admin_label'] = [ | |
160 '#type' => 'item', | |
161 '#title' => $this->t('Block description'), | |
162 '#plain_text' => $definition['admin_label'], | |
163 ]; | |
164 $form['label'] = [ | |
165 '#type' => 'textfield', | |
166 '#title' => $this->t('Title'), | |
167 '#maxlength' => 255, | |
168 '#default_value' => $this->label(), | |
169 '#required' => TRUE, | |
170 ]; | |
171 $form['label_display'] = [ | |
172 '#type' => 'checkbox', | |
173 '#title' => $this->t('Display title'), | |
174 '#default_value' => ($this->configuration['label_display'] === static::BLOCK_LABEL_VISIBLE), | |
175 '#return_value' => static::BLOCK_LABEL_VISIBLE, | |
176 ]; | |
177 | |
178 // Add context mapping UI form elements. | |
179 $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: []; | |
180 $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts); | |
181 // Add plugin-specific settings for this block type. | |
182 $form += $this->blockForm($form, $form_state); | |
183 return $form; | |
184 } | |
185 | |
186 /** | |
187 * {@inheritdoc} | |
188 */ | |
189 public function blockForm($form, FormStateInterface $form_state) { | |
190 return []; | |
191 } | |
192 | |
193 /** | |
194 * {@inheritdoc} | |
195 * | |
196 * Most block plugins should not override this method. To add validation | |
197 * for a specific block type, override BlockBase::blockValidate(). | |
198 * | |
199 * @see \Drupal\Core\Block\BlockBase::blockValidate() | |
200 */ | |
201 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { | |
202 // Remove the admin_label form item element value so it will not persist. | |
203 $form_state->unsetValue('admin_label'); | |
204 | |
205 $this->blockValidate($form, $form_state); | |
206 } | |
207 | |
208 /** | |
209 * {@inheritdoc} | |
210 */ | |
211 public function blockValidate($form, FormStateInterface $form_state) {} | |
212 | |
213 /** | |
214 * {@inheritdoc} | |
215 * | |
216 * Most block plugins should not override this method. To add submission | |
217 * handling for a specific block type, override BlockBase::blockSubmit(). | |
218 * | |
219 * @see \Drupal\Core\Block\BlockBase::blockSubmit() | |
220 */ | |
221 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
222 // Process the block's submission handling if no errors occurred only. | |
223 if (!$form_state->getErrors()) { | |
224 $this->configuration['label'] = $form_state->getValue('label'); | |
225 $this->configuration['label_display'] = $form_state->getValue('label_display'); | |
226 $this->configuration['provider'] = $form_state->getValue('provider'); | |
227 $this->blockSubmit($form, $form_state); | |
228 } | |
229 } | |
230 | |
231 /** | |
232 * {@inheritdoc} | |
233 */ | |
234 public function blockSubmit($form, FormStateInterface $form_state) {} | |
235 | |
236 /** | |
237 * {@inheritdoc} | |
238 */ | |
239 public function getMachineNameSuggestion() { | |
240 $definition = $this->getPluginDefinition(); | |
241 $admin_label = $definition['admin_label']; | |
242 | |
243 // @todo This is basically the same as what is done in | |
244 // \Drupal\system\MachineNameController::transliterate(), so it might make | |
245 // sense to provide a common service for the two. | |
246 $transliterated = $this->transliteration()->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_'); | |
247 $transliterated = Unicode::strtolower($transliterated); | |
248 | |
249 $transliterated = preg_replace('@[^a-z0-9_.]+@', '', $transliterated); | |
250 | |
251 return $transliterated; | |
252 } | |
253 | |
254 /** | |
255 * Wraps the transliteration service. | |
256 * | |
257 * @return \Drupal\Component\Transliteration\TransliterationInterface | |
258 */ | |
259 protected function transliteration() { | |
260 if (!$this->transliteration) { | |
261 $this->transliteration = \Drupal::transliteration(); | |
262 } | |
263 return $this->transliteration; | |
264 } | |
265 | |
266 /** | |
267 * Sets the transliteration service. | |
268 * | |
269 * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration | |
270 * The transliteration service. | |
271 */ | |
272 public function setTransliteration(TransliterationInterface $transliteration) { | |
273 $this->transliteration = $transliteration; | |
274 } | |
275 | |
276 } |