Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Common functions that many Drupal modules will need to reference.
|
Chris@0
|
6 *
|
Chris@0
|
7 * The functions that are critical and need to be available even when serving
|
Chris@0
|
8 * a cached page are instead located in bootstrap.inc.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 use Drupal\Component\Serialization\Json;
|
Chris@0
|
12 use Drupal\Component\Utility\Bytes;
|
Chris@0
|
13 use Drupal\Component\Utility\Html;
|
Chris@0
|
14 use Drupal\Component\Utility\SortArray;
|
Chris@0
|
15 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
16 use Drupal\Core\Cache\Cache;
|
Chris@0
|
17 use Drupal\Core\Render\Element\Link;
|
Chris@0
|
18 use Drupal\Core\Render\Markup;
|
Chris@0
|
19 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@0
|
20 use Drupal\Core\PhpStorage\PhpStorageFactory;
|
Chris@0
|
21 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
|
Chris@0
|
22 use Drupal\Core\Render\BubbleableMetadata;
|
Chris@0
|
23 use Drupal\Core\Render\Element;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * @defgroup php_wrappers PHP wrapper functions
|
Chris@0
|
27 * @{
|
Chris@0
|
28 * Functions that are wrappers or custom implementations of PHP functions.
|
Chris@0
|
29 *
|
Chris@0
|
30 * Certain PHP functions should not be used in Drupal. Instead, Drupal's
|
Chris@0
|
31 * replacement functions should be used.
|
Chris@0
|
32 *
|
Chris@0
|
33 * For example, for improved or more secure UTF8-handling, or RFC-compliant
|
Chris@0
|
34 * handling of URLs in Drupal.
|
Chris@0
|
35 *
|
Chris@0
|
36 * For ease of use and memorizing, all these wrapper functions use the same name
|
Chris@0
|
37 * as the original PHP function, but prefixed with "drupal_". Beware, however,
|
Chris@0
|
38 * that not all wrapper functions support the same arguments as the original
|
Chris@0
|
39 * functions.
|
Chris@0
|
40 *
|
Chris@0
|
41 * You should always use these wrapper functions in your code.
|
Chris@0
|
42 *
|
Chris@0
|
43 * Wrong:
|
Chris@0
|
44 * @code
|
Chris@0
|
45 * $my_substring = substr($original_string, 0, 5);
|
Chris@0
|
46 * @endcode
|
Chris@0
|
47 *
|
Chris@0
|
48 * Correct:
|
Chris@0
|
49 * @code
|
Chris@0
|
50 * $my_substring = Unicode::substr($original_string, 0, 5);
|
Chris@0
|
51 * @endcode
|
Chris@0
|
52 *
|
Chris@0
|
53 * @}
|
Chris@0
|
54 */
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Return status for saving which involved creating a new item.
|
Chris@0
|
58 */
|
Chris@0
|
59 const SAVED_NEW = 1;
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Return status for saving which involved an update to an existing item.
|
Chris@0
|
63 */
|
Chris@0
|
64 const SAVED_UPDATED = 2;
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Return status for saving which deleted an existing item.
|
Chris@0
|
68 */
|
Chris@0
|
69 const SAVED_DELETED = 3;
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * The default aggregation group for CSS files added to the page.
|
Chris@0
|
73 */
|
Chris@0
|
74 const CSS_AGGREGATE_DEFAULT = 0;
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * The default aggregation group for theme CSS files added to the page.
|
Chris@0
|
78 */
|
Chris@0
|
79 const CSS_AGGREGATE_THEME = 100;
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * The default weight for CSS rules that style HTML elements ("base" styles).
|
Chris@0
|
83 */
|
Chris@0
|
84 const CSS_BASE = -200;
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * The default weight for CSS rules that layout a page.
|
Chris@0
|
88 */
|
Chris@0
|
89 const CSS_LAYOUT = -100;
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * The default weight for CSS rules that style design components (and their associated states and themes.)
|
Chris@0
|
93 */
|
Chris@0
|
94 const CSS_COMPONENT = 0;
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * The default weight for CSS rules that style states and are not included with components.
|
Chris@0
|
98 */
|
Chris@0
|
99 const CSS_STATE = 100;
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * The default weight for CSS rules that style themes and are not included with components.
|
Chris@0
|
103 */
|
Chris@0
|
104 const CSS_THEME = 200;
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * The default group for JavaScript settings added to the page.
|
Chris@0
|
108 */
|
Chris@0
|
109 const JS_SETTING = -200;
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * The default group for JavaScript and jQuery libraries added to the page.
|
Chris@0
|
113 */
|
Chris@0
|
114 const JS_LIBRARY = -100;
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * The default group for module JavaScript code added to the page.
|
Chris@0
|
118 */
|
Chris@0
|
119 const JS_DEFAULT = 0;
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * The default group for theme JavaScript code added to the page.
|
Chris@0
|
123 */
|
Chris@0
|
124 const JS_THEME = 100;
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * The delimiter used to split plural strings.
|
Chris@0
|
128 *
|
Chris@0
|
129 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
|
Chris@0
|
130 * Use \Drupal\Core\StringTranslation\PluralTranslatableMarkup::DELIMITER
|
Chris@0
|
131 * instead.
|
Chris@0
|
132 */
|
Chris@0
|
133 const LOCALE_PLURAL_DELIMITER = PluralTranslatableMarkup::DELIMITER;
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Prepares a 'destination' URL query parameter.
|
Chris@0
|
137 *
|
Chris@0
|
138 * Used to direct the user back to the referring page after completing a form.
|
Chris@0
|
139 * By default the current URL is returned. If a destination exists in the
|
Chris@0
|
140 * previous request, that destination is returned. As such, a destination can
|
Chris@0
|
141 * persist across multiple pages.
|
Chris@0
|
142 *
|
Chris@0
|
143 * @return array
|
Chris@0
|
144 * An associative array containing the key:
|
Chris@0
|
145 * - destination: The value of the current request's 'destination' query
|
Chris@0
|
146 * parameter, if present. This can be either a relative or absolute URL.
|
Chris@0
|
147 * However, for security, redirection to external URLs is not performed.
|
Chris@0
|
148 * If the query parameter isn't present, then the URL of the current
|
Chris@0
|
149 * request is returned.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @ingroup form_api
|
Chris@0
|
152 *
|
Chris@0
|
153 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
|
Chris@0
|
154 * Use the redirect.destination service.
|
Chris@12
|
155 *
|
Chris@12
|
156 * @see \Drupal\Core\EventSubscriber\RedirectResponseSubscriber::checkRedirectUrl()
|
Chris@12
|
157 * @see https://www.drupal.org/node/2448603
|
Chris@0
|
158 */
|
Chris@0
|
159 function drupal_get_destination() {
|
Chris@0
|
160 return \Drupal::destination()->getAsArray();
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@0
|
164 * @defgroup validation Input validation
|
Chris@0
|
165 * @{
|
Chris@0
|
166 * Functions to validate user input.
|
Chris@0
|
167 */
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * Verifies the syntax of the given email address.
|
Chris@0
|
171 *
|
Chris@0
|
172 * @param string $mail
|
Chris@0
|
173 * A string containing an email address.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @return bool
|
Chris@0
|
176 * TRUE if the address is in a valid format.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
|
Chris@0
|
179 * Use \Drupal::service('email.validator')->isValid().
|
Chris@12
|
180 *
|
Chris@12
|
181 * @see https://www.drupal.org/node/2912661
|
Chris@0
|
182 */
|
Chris@0
|
183 function valid_email_address($mail) {
|
Chris@0
|
184 return \Drupal::service('email.validator')->isValid($mail);
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * @} End of "defgroup validation".
|
Chris@0
|
189 */
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * @defgroup sanitization Sanitization functions
|
Chris@0
|
193 * @{
|
Chris@0
|
194 * Functions to sanitize values.
|
Chris@0
|
195 *
|
Chris@0
|
196 * See https://www.drupal.org/writing-secure-code for information
|
Chris@0
|
197 * on writing secure code.
|
Chris@0
|
198 */
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Strips dangerous protocols from a URI and encodes it for output to HTML.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @param $uri
|
Chris@0
|
204 * A plain-text URI that might contain dangerous protocols.
|
Chris@0
|
205 *
|
Chris@0
|
206 * @return string
|
Chris@0
|
207 * A URI stripped of dangerous protocols and encoded for output to an HTML
|
Chris@0
|
208 * attribute value. Because it is already encoded, it should not be set as a
|
Chris@0
|
209 * value within a $attributes array passed to Drupal\Core\Template\Attribute,
|
Chris@0
|
210 * because Drupal\Core\Template\Attribute expects those values to be
|
Chris@0
|
211 * plain-text strings. To pass a filtered URI to
|
Chris@0
|
212 * Drupal\Core\Template\Attribute, call
|
Chris@0
|
213 * \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() instead.
|
Chris@0
|
214 *
|
Chris@0
|
215 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
|
Chris@0
|
216 * Use UrlHelper::stripDangerousProtocols() or UrlHelper::filterBadProtocol()
|
Chris@0
|
217 * instead. UrlHelper::stripDangerousProtocols() can be used in conjunction
|
Chris@0
|
218 * with \Drupal\Component\Utility\SafeMarkup::format() and an @variable
|
Chris@0
|
219 * placeholder which will perform the necessary escaping.
|
Chris@0
|
220 * UrlHelper::filterBadProtocol() is functionality equivalent to check_url()
|
Chris@0
|
221 * apart from the fact it is protected from double escaping bugs. Note that
|
Chris@0
|
222 * this method no longer marks its output as safe.
|
Chris@12
|
223 *
|
Chris@12
|
224 * @see \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols()
|
Chris@12
|
225 * @see \Drupal\Component\Utility\UrlHelper::filterBadProtocol()
|
Chris@12
|
226 * @see https://www.drupal.org/node/2560027
|
Chris@0
|
227 */
|
Chris@0
|
228 function check_url($uri) {
|
Chris@0
|
229 return Html::escape(UrlHelper::stripDangerousProtocols($uri));
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 /**
|
Chris@0
|
233 * @} End of "defgroup sanitization".
|
Chris@0
|
234 */
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * @defgroup format Formatting
|
Chris@0
|
238 * @{
|
Chris@0
|
239 * Functions to format numbers, strings, dates, etc.
|
Chris@0
|
240 */
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * Generates a string representation for the given byte count.
|
Chris@0
|
244 *
|
Chris@0
|
245 * @param $size
|
Chris@0
|
246 * A size in bytes.
|
Chris@0
|
247 * @param $langcode
|
Chris@0
|
248 * Optional language code to translate to a language other than what is used
|
Chris@0
|
249 * to display the page.
|
Chris@0
|
250 *
|
Chris@0
|
251 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
|
Chris@0
|
252 * A translated string representation of the size.
|
Chris@0
|
253 */
|
Chris@0
|
254 function format_size($size, $langcode = NULL) {
|
Chris@0
|
255 if ($size < Bytes::KILOBYTE) {
|
Chris@0
|
256 return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]);
|
Chris@0
|
257 }
|
Chris@0
|
258 else {
|
Chris@0
|
259 // Convert bytes to kilobytes.
|
Chris@0
|
260 $size = $size / Bytes::KILOBYTE;
|
Chris@0
|
261 $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
Chris@0
|
262 foreach ($units as $unit) {
|
Chris@0
|
263 if (round($size, 2) >= Bytes::KILOBYTE) {
|
Chris@0
|
264 $size = $size / Bytes::KILOBYTE;
|
Chris@0
|
265 }
|
Chris@0
|
266 else {
|
Chris@0
|
267 break;
|
Chris@0
|
268 }
|
Chris@0
|
269 }
|
Chris@0
|
270 $args = ['@size' => round($size, 2)];
|
Chris@0
|
271 $options = ['langcode' => $langcode];
|
Chris@0
|
272 switch ($unit) {
|
Chris@0
|
273 case 'KB':
|
Chris@0
|
274 return new TranslatableMarkup('@size KB', $args, $options);
|
Chris@0
|
275 case 'MB':
|
Chris@0
|
276 return new TranslatableMarkup('@size MB', $args, $options);
|
Chris@0
|
277 case 'GB':
|
Chris@0
|
278 return new TranslatableMarkup('@size GB', $args, $options);
|
Chris@0
|
279 case 'TB':
|
Chris@0
|
280 return new TranslatableMarkup('@size TB', $args, $options);
|
Chris@0
|
281 case 'PB':
|
Chris@0
|
282 return new TranslatableMarkup('@size PB', $args, $options);
|
Chris@0
|
283 case 'EB':
|
Chris@0
|
284 return new TranslatableMarkup('@size EB', $args, $options);
|
Chris@0
|
285 case 'ZB':
|
Chris@0
|
286 return new TranslatableMarkup('@size ZB', $args, $options);
|
Chris@0
|
287 case 'YB':
|
Chris@0
|
288 return new TranslatableMarkup('@size YB', $args, $options);
|
Chris@0
|
289 }
|
Chris@0
|
290 }
|
Chris@0
|
291 }
|
Chris@0
|
292
|
Chris@0
|
293 /**
|
Chris@0
|
294 * Formats a date, using a date type or a custom date format string.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @param $timestamp
|
Chris@0
|
297 * A UNIX timestamp to format.
|
Chris@0
|
298 * @param $type
|
Chris@0
|
299 * (optional) The format to use, one of:
|
Chris@0
|
300 * - One of the built-in formats: 'short', 'medium',
|
Chris@0
|
301 * 'long', 'html_datetime', 'html_date', 'html_time',
|
Chris@0
|
302 * 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
|
Chris@0
|
303 * - The name of a date type defined by a date format config entity.
|
Chris@0
|
304 * - The machine name of an administrator-defined date format.
|
Chris@0
|
305 * - 'custom', to use $format.
|
Chris@0
|
306 * Defaults to 'medium'.
|
Chris@0
|
307 * @param $format
|
Chris@0
|
308 * (optional) If $type is 'custom', a PHP date format string suitable for
|
Chris@0
|
309 * input to date(). Use a backslash to escape ordinary text, so it does not
|
Chris@0
|
310 * get interpreted as date format characters.
|
Chris@0
|
311 * @param $timezone
|
Chris@0
|
312 * (optional) Time zone identifier, as described at
|
Chris@0
|
313 * http://php.net/manual/timezones.php Defaults to the time zone used to
|
Chris@0
|
314 * display the page.
|
Chris@0
|
315 * @param $langcode
|
Chris@0
|
316 * (optional) Language code to translate to. Defaults to the language used to
|
Chris@0
|
317 * display the page.
|
Chris@0
|
318 *
|
Chris@0
|
319 * @return
|
Chris@0
|
320 * A translated date string in the requested format.
|
Chris@0
|
321 *
|
Chris@0
|
322 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
323 * Use \Drupal::service('date.formatter')->format().
|
Chris@12
|
324 *
|
Chris@12
|
325 * @see \Drupal\Core\Datetime\DateFormatter::format()
|
Chris@12
|
326 * @see https://www.drupal.org/node/1876852
|
Chris@0
|
327 */
|
Chris@0
|
328 function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
|
Chris@0
|
329 return \Drupal::service('date.formatter')->format($timestamp, $type, $format, $timezone, $langcode);
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 /**
|
Chris@0
|
333 * Returns an ISO8601 formatted date based on the given date.
|
Chris@0
|
334 *
|
Chris@0
|
335 * @param $date
|
Chris@0
|
336 * A UNIX timestamp.
|
Chris@0
|
337 *
|
Chris@0
|
338 * @return string
|
Chris@0
|
339 * An ISO8601 formatted date.
|
Chris@0
|
340 */
|
Chris@0
|
341 function date_iso8601($date) {
|
Chris@0
|
342 // The DATE_ISO8601 constant cannot be used here because it does not match
|
Chris@0
|
343 // date('c') and produces invalid RDF markup.
|
Chris@0
|
344 return date('c', $date);
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 /**
|
Chris@0
|
348 * @} End of "defgroup format".
|
Chris@0
|
349 */
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * Formats an attribute string for an HTTP header.
|
Chris@0
|
353 *
|
Chris@0
|
354 * @param $attributes
|
Chris@0
|
355 * An associative array of attributes such as 'rel'.
|
Chris@0
|
356 *
|
Chris@0
|
357 * @return
|
Chris@0
|
358 * A ; separated string ready for insertion in a HTTP header. No escaping is
|
Chris@0
|
359 * performed for HTML entities, so this string is not safe to be printed.
|
Chris@0
|
360 */
|
Chris@0
|
361 function drupal_http_header_attributes(array $attributes = []) {
|
Chris@0
|
362 foreach ($attributes as $attribute => &$data) {
|
Chris@0
|
363 if (is_array($data)) {
|
Chris@0
|
364 $data = implode(' ', $data);
|
Chris@0
|
365 }
|
Chris@0
|
366 $data = $attribute . '="' . $data . '"';
|
Chris@0
|
367 }
|
Chris@0
|
368 return $attributes ? ' ' . implode('; ', $attributes) : '';
|
Chris@0
|
369 }
|
Chris@0
|
370
|
Chris@0
|
371 /**
|
Chris@0
|
372 * Attempts to set the PHP maximum execution time.
|
Chris@0
|
373 *
|
Chris@0
|
374 * This function is a wrapper around the PHP function set_time_limit().
|
Chris@0
|
375 * When called, set_time_limit() restarts the timeout counter from zero.
|
Chris@0
|
376 * In other words, if the timeout is the default 30 seconds, and 25 seconds
|
Chris@0
|
377 * into script execution a call such as set_time_limit(20) is made, the
|
Chris@0
|
378 * script will run for a total of 45 seconds before timing out.
|
Chris@0
|
379 *
|
Chris@0
|
380 * If the current time limit is not unlimited it is possible to decrease the
|
Chris@0
|
381 * total time limit if the sum of the new time limit and the current time spent
|
Chris@0
|
382 * running the script is inferior to the original time limit. It is inherent to
|
Chris@0
|
383 * the way set_time_limit() works, it should rather be called with an
|
Chris@0
|
384 * appropriate value every time you need to allocate a certain amount of time
|
Chris@0
|
385 * to execute a task than only once at the beginning of the script.
|
Chris@0
|
386 *
|
Chris@0
|
387 * Before calling set_time_limit(), we check if this function is available
|
Chris@0
|
388 * because it could be disabled by the server administrator. We also hide all
|
Chris@0
|
389 * the errors that could occur when calling set_time_limit(), because it is
|
Chris@0
|
390 * not possible to reliably ensure that PHP or a security extension will
|
Chris@0
|
391 * not issue a warning/error if they prevent the use of this function.
|
Chris@0
|
392 *
|
Chris@0
|
393 * @param $time_limit
|
Chris@0
|
394 * An integer specifying the new time limit, in seconds. A value of 0
|
Chris@0
|
395 * indicates unlimited execution time.
|
Chris@0
|
396 *
|
Chris@0
|
397 * @ingroup php_wrappers
|
Chris@0
|
398 */
|
Chris@0
|
399 function drupal_set_time_limit($time_limit) {
|
Chris@0
|
400 if (function_exists('set_time_limit')) {
|
Chris@0
|
401 $current = ini_get('max_execution_time');
|
Chris@0
|
402 // Do not set time limit if it is currently unlimited.
|
Chris@0
|
403 if ($current != 0) {
|
Chris@0
|
404 @set_time_limit($time_limit);
|
Chris@0
|
405 }
|
Chris@0
|
406 }
|
Chris@0
|
407 }
|
Chris@0
|
408
|
Chris@0
|
409 /**
|
Chris@0
|
410 * Returns the base URL path (i.e., directory) of the Drupal installation.
|
Chris@0
|
411 *
|
Chris@0
|
412 * base_path() adds a "/" to the beginning and end of the returned path if the
|
Chris@0
|
413 * path is not empty. At the very least, this will return "/".
|
Chris@0
|
414 *
|
Chris@0
|
415 * Examples:
|
Chris@0
|
416 * - http://example.com returns "/" because the path is empty.
|
Chris@0
|
417 * - http://example.com/drupal/folder returns "/drupal/folder/".
|
Chris@0
|
418 */
|
Chris@0
|
419 function base_path() {
|
Chris@0
|
420 return $GLOBALS['base_path'];
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 /**
|
Chris@0
|
424 * Deletes old cached CSS files.
|
Chris@0
|
425 *
|
Chris@0
|
426 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
|
Chris@0
|
427 * Use \Drupal\Core\Asset\AssetCollectionOptimizerInterface::deleteAll().
|
Chris@12
|
428 *
|
Chris@12
|
429 * @see https://www.drupal.org/node/2317841
|
Chris@0
|
430 */
|
Chris@0
|
431 function drupal_clear_css_cache() {
|
Chris@0
|
432 \Drupal::service('asset.css.collection_optimizer')->deleteAll();
|
Chris@0
|
433 }
|
Chris@0
|
434
|
Chris@0
|
435 /**
|
Chris@0
|
436 * Constructs an array of the defaults that are used for JavaScript assets.
|
Chris@0
|
437 *
|
Chris@0
|
438 * @param $data
|
Chris@0
|
439 * (optional) The default data parameter for the JavaScript asset array.
|
Chris@0
|
440 *
|
Chris@0
|
441 * @see hook_js_alter()
|
Chris@0
|
442 */
|
Chris@0
|
443 function drupal_js_defaults($data = NULL) {
|
Chris@0
|
444 return [
|
Chris@0
|
445 'type' => 'file',
|
Chris@0
|
446 'group' => JS_DEFAULT,
|
Chris@0
|
447 'weight' => 0,
|
Chris@0
|
448 'scope' => 'header',
|
Chris@0
|
449 'cache' => TRUE,
|
Chris@0
|
450 'preprocess' => TRUE,
|
Chris@0
|
451 'attributes' => [],
|
Chris@0
|
452 'version' => NULL,
|
Chris@0
|
453 'data' => $data,
|
Chris@0
|
454 'browsers' => [],
|
Chris@0
|
455 ];
|
Chris@0
|
456 }
|
Chris@0
|
457
|
Chris@0
|
458 /**
|
Chris@0
|
459 * Adds JavaScript to change the state of an element based on another element.
|
Chris@0
|
460 *
|
Chris@0
|
461 * A "state" means a certain property on a DOM element, such as "visible" or
|
Chris@0
|
462 * "checked". A state can be applied to an element, depending on the state of
|
Chris@0
|
463 * another element on the page. In general, states depend on HTML attributes and
|
Chris@0
|
464 * DOM element properties, which change due to user interaction.
|
Chris@0
|
465 *
|
Chris@0
|
466 * Since states are driven by JavaScript only, it is important to understand
|
Chris@0
|
467 * that all states are applied on presentation only, none of the states force
|
Chris@0
|
468 * any server-side logic, and that they will not be applied for site visitors
|
Chris@0
|
469 * without JavaScript support. All modules implementing states have to make
|
Chris@0
|
470 * sure that the intended logic also works without JavaScript being enabled.
|
Chris@0
|
471 *
|
Chris@0
|
472 * #states is an associative array in the form of:
|
Chris@0
|
473 * @code
|
Chris@0
|
474 * array(
|
Chris@0
|
475 * STATE1 => CONDITIONS_ARRAY1,
|
Chris@0
|
476 * STATE2 => CONDITIONS_ARRAY2,
|
Chris@0
|
477 * ...
|
Chris@0
|
478 * )
|
Chris@0
|
479 * @endcode
|
Chris@0
|
480 * Each key is the name of a state to apply to the element, such as 'visible'.
|
Chris@0
|
481 * Each value is a list of conditions that denote when the state should be
|
Chris@0
|
482 * applied.
|
Chris@0
|
483 *
|
Chris@0
|
484 * Multiple different states may be specified to act on complex conditions:
|
Chris@0
|
485 * @code
|
Chris@0
|
486 * array(
|
Chris@0
|
487 * 'visible' => CONDITIONS,
|
Chris@0
|
488 * 'checked' => OTHER_CONDITIONS,
|
Chris@0
|
489 * )
|
Chris@0
|
490 * @endcode
|
Chris@0
|
491 *
|
Chris@0
|
492 * Every condition is a key/value pair, whose key is a jQuery selector that
|
Chris@0
|
493 * denotes another element on the page, and whose value is an array of
|
Chris@0
|
494 * conditions, which must bet met on that element:
|
Chris@0
|
495 * @code
|
Chris@0
|
496 * array(
|
Chris@0
|
497 * 'visible' => array(
|
Chris@0
|
498 * JQUERY_SELECTOR => REMOTE_CONDITIONS,
|
Chris@0
|
499 * JQUERY_SELECTOR => REMOTE_CONDITIONS,
|
Chris@0
|
500 * ...
|
Chris@0
|
501 * ),
|
Chris@0
|
502 * )
|
Chris@0
|
503 * @endcode
|
Chris@0
|
504 * All conditions must be met for the state to be applied.
|
Chris@0
|
505 *
|
Chris@0
|
506 * Each remote condition is a key/value pair specifying conditions on the other
|
Chris@0
|
507 * element that need to be met to apply the state to the element:
|
Chris@0
|
508 * @code
|
Chris@0
|
509 * array(
|
Chris@0
|
510 * 'visible' => array(
|
Chris@0
|
511 * ':input[name="remote_checkbox"]' => array('checked' => TRUE),
|
Chris@0
|
512 * ),
|
Chris@0
|
513 * )
|
Chris@0
|
514 * @endcode
|
Chris@0
|
515 *
|
Chris@0
|
516 * For example, to show a textfield only when a checkbox is checked:
|
Chris@0
|
517 * @code
|
Chris@0
|
518 * $form['toggle_me'] = array(
|
Chris@0
|
519 * '#type' => 'checkbox',
|
Chris@0
|
520 * '#title' => t('Tick this box to type'),
|
Chris@0
|
521 * );
|
Chris@0
|
522 * $form['settings'] = array(
|
Chris@0
|
523 * '#type' => 'textfield',
|
Chris@0
|
524 * '#states' => array(
|
Chris@0
|
525 * // Only show this field when the 'toggle_me' checkbox is enabled.
|
Chris@0
|
526 * 'visible' => array(
|
Chris@0
|
527 * ':input[name="toggle_me"]' => array('checked' => TRUE),
|
Chris@0
|
528 * ),
|
Chris@0
|
529 * ),
|
Chris@0
|
530 * );
|
Chris@0
|
531 * @endcode
|
Chris@0
|
532 *
|
Chris@0
|
533 * The following states may be applied to an element:
|
Chris@0
|
534 * - enabled
|
Chris@0
|
535 * - disabled
|
Chris@0
|
536 * - required
|
Chris@0
|
537 * - optional
|
Chris@0
|
538 * - visible
|
Chris@0
|
539 * - invisible
|
Chris@0
|
540 * - checked
|
Chris@0
|
541 * - unchecked
|
Chris@0
|
542 * - expanded
|
Chris@0
|
543 * - collapsed
|
Chris@0
|
544 *
|
Chris@0
|
545 * The following states may be used in remote conditions:
|
Chris@0
|
546 * - empty
|
Chris@0
|
547 * - filled
|
Chris@0
|
548 * - checked
|
Chris@0
|
549 * - unchecked
|
Chris@0
|
550 * - expanded
|
Chris@0
|
551 * - collapsed
|
Chris@0
|
552 * - value
|
Chris@0
|
553 *
|
Chris@0
|
554 * The following states exist for both elements and remote conditions, but are
|
Chris@0
|
555 * not fully implemented and may not change anything on the element:
|
Chris@0
|
556 * - relevant
|
Chris@0
|
557 * - irrelevant
|
Chris@0
|
558 * - valid
|
Chris@0
|
559 * - invalid
|
Chris@0
|
560 * - touched
|
Chris@0
|
561 * - untouched
|
Chris@0
|
562 * - readwrite
|
Chris@0
|
563 * - readonly
|
Chris@0
|
564 *
|
Chris@0
|
565 * When referencing select lists and radio buttons in remote conditions, a
|
Chris@0
|
566 * 'value' condition must be used:
|
Chris@0
|
567 * @code
|
Chris@0
|
568 * '#states' => array(
|
Chris@0
|
569 * // Show the settings if 'bar' has been selected for 'foo'.
|
Chris@0
|
570 * 'visible' => array(
|
Chris@0
|
571 * ':input[name="foo"]' => array('value' => 'bar'),
|
Chris@0
|
572 * ),
|
Chris@0
|
573 * ),
|
Chris@0
|
574 * @endcode
|
Chris@0
|
575 *
|
Chris@0
|
576 * @param $elements
|
Chris@0
|
577 * A renderable array element having a #states property as described above.
|
Chris@0
|
578 *
|
Chris@0
|
579 * @see form_example_states_form()
|
Chris@0
|
580 */
|
Chris@0
|
581 function drupal_process_states(&$elements) {
|
Chris@0
|
582 $elements['#attached']['library'][] = 'core/drupal.states';
|
Chris@0
|
583 // Elements of '#type' => 'item' are not actual form input elements, but we
|
Chris@0
|
584 // still want to be able to show/hide them. Since there's no actual HTML input
|
Chris@0
|
585 // element available, setting #attributes does not make sense, but a wrapper
|
Chris@0
|
586 // is available, so setting #wrapper_attributes makes it work.
|
Chris@0
|
587 $key = ($elements['#type'] == 'item') ? '#wrapper_attributes' : '#attributes';
|
Chris@0
|
588 $elements[$key]['data-drupal-states'] = Json::encode($elements['#states']);
|
Chris@0
|
589 }
|
Chris@0
|
590
|
Chris@0
|
591 /**
|
Chris@0
|
592 * Assists in attaching the tableDrag JavaScript behavior to a themed table.
|
Chris@0
|
593 *
|
Chris@0
|
594 * Draggable tables should be used wherever an outline or list of sortable items
|
Chris@0
|
595 * needs to be arranged by an end-user. Draggable tables are very flexible and
|
Chris@0
|
596 * can manipulate the value of form elements placed within individual columns.
|
Chris@0
|
597 *
|
Chris@0
|
598 * To set up a table to use drag and drop in place of weight select-lists or in
|
Chris@0
|
599 * place of a form that contains parent relationships, the form must be themed
|
Chris@0
|
600 * into a table. The table must have an ID attribute set and it
|
Chris@0
|
601 * may be set as follows:
|
Chris@0
|
602 * @code
|
Chris@0
|
603 * $table = array(
|
Chris@0
|
604 * '#type' => 'table',
|
Chris@0
|
605 * '#header' => $header,
|
Chris@0
|
606 * '#rows' => $rows,
|
Chris@0
|
607 * '#attributes' => array(
|
Chris@0
|
608 * 'id' => 'my-module-table',
|
Chris@0
|
609 * ),
|
Chris@0
|
610 * );
|
Chris@16
|
611 * return \Drupal::service('renderer')->render($table);
|
Chris@0
|
612 * @endcode
|
Chris@0
|
613 *
|
Chris@0
|
614 * In the theme function for the form, a special class must be added to each
|
Chris@0
|
615 * form element within the same column, "grouping" them together.
|
Chris@0
|
616 *
|
Chris@0
|
617 * In a situation where a single weight column is being sorted in the table, the
|
Chris@0
|
618 * classes could be added like this (in the theme function):
|
Chris@0
|
619 * @code
|
Chris@0
|
620 * $form['my_elements'][$delta]['weight']['#attributes']['class'] = array('my-elements-weight');
|
Chris@0
|
621 * @endcode
|
Chris@0
|
622 *
|
Chris@0
|
623 * Each row of the table must also have a class of "draggable" in order to
|
Chris@0
|
624 * enable the drag handles:
|
Chris@0
|
625 * @code
|
Chris@0
|
626 * $row = array(...);
|
Chris@0
|
627 * $rows[] = array(
|
Chris@0
|
628 * 'data' => $row,
|
Chris@0
|
629 * 'class' => array('draggable'),
|
Chris@0
|
630 * );
|
Chris@0
|
631 * @endcode
|
Chris@0
|
632 *
|
Chris@0
|
633 * When tree relationships are present, the two additional classes
|
Chris@0
|
634 * 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the behavior:
|
Chris@0
|
635 * - Rows with the 'tabledrag-leaf' class cannot have child rows.
|
Chris@0
|
636 * - Rows with the 'tabledrag-root' class cannot be nested under a parent row.
|
Chris@0
|
637 *
|
Chris@0
|
638 * Calling drupal_attach_tabledrag() would then be written as such:
|
Chris@0
|
639 * @code
|
Chris@0
|
640 * drupal_attach_tabledrag('my-module-table', array(
|
Chris@0
|
641 * 'action' => 'order',
|
Chris@0
|
642 * 'relationship' => 'sibling',
|
Chris@0
|
643 * 'group' => 'my-elements-weight',
|
Chris@0
|
644 * );
|
Chris@0
|
645 * @endcode
|
Chris@0
|
646 *
|
Chris@0
|
647 * In a more complex case where there are several groups in one column (such as
|
Chris@0
|
648 * the block regions on the admin/structure/block page), a separate subgroup
|
Chris@0
|
649 * class must also be added to differentiate the groups.
|
Chris@0
|
650 * @code
|
Chris@0
|
651 * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight', 'my-elements-weight-' . $region);
|
Chris@0
|
652 * @endcode
|
Chris@0
|
653 *
|
Chris@0
|
654 * The 'group' option is still 'my-element-weight', and the additional
|
Chris@0
|
655 * 'subgroup' option will be passed in as 'my-elements-weight-' . $region. This
|
Chris@0
|
656 * also means that you'll need to call drupal_attach_tabledrag() once for every
|
Chris@0
|
657 * region added.
|
Chris@0
|
658 *
|
Chris@0
|
659 * @code
|
Chris@0
|
660 * foreach ($regions as $region) {
|
Chris@0
|
661 * drupal_attach_tabledrag('my-module-table', array(
|
Chris@0
|
662 * 'action' => 'order',
|
Chris@0
|
663 * 'relationship' => 'sibling',
|
Chris@0
|
664 * 'group' => 'my-elements-weight',
|
Chris@0
|
665 * 'subgroup' => 'my-elements-weight-' . $region,
|
Chris@0
|
666 * ));
|
Chris@0
|
667 * }
|
Chris@0
|
668 * @endcode
|
Chris@0
|
669 *
|
Chris@0
|
670 * In a situation where tree relationships are present, adding multiple
|
Chris@0
|
671 * subgroups is not necessary, because the table will contain indentations that
|
Chris@0
|
672 * provide enough information about the sibling and parent relationships. See
|
Chris@0
|
673 * MenuForm::BuildOverviewForm for an example creating a table
|
Chris@0
|
674 * containing parent relationships.
|
Chris@0
|
675 *
|
Chris@0
|
676 * @param $element
|
Chris@0
|
677 * A form element to attach the tableDrag behavior to.
|
Chris@0
|
678 * @param array $options
|
Chris@0
|
679 * These options are used to generate JavaScript settings necessary to
|
Chris@0
|
680 * configure the tableDrag behavior appropriately for this particular table.
|
Chris@0
|
681 * An associative array containing the following keys:
|
Chris@0
|
682 * - 'table_id': String containing the target table's id attribute.
|
Chris@0
|
683 * If the table does not have an id, one will need to be set,
|
Chris@0
|
684 * such as <table id="my-module-table">.
|
Chris@0
|
685 * - 'action': String describing the action to be done on the form item.
|
Chris@0
|
686 * Either 'match' 'depth', or 'order':
|
Chris@0
|
687 * - 'match' is typically used for parent relationships.
|
Chris@0
|
688 * - 'order' is typically used to set weights on other form elements with
|
Chris@0
|
689 * the same group.
|
Chris@0
|
690 * - 'depth' updates the target element with the current indentation.
|
Chris@0
|
691 * - 'relationship': String describing where the "action" option
|
Chris@0
|
692 * should be performed. Either 'parent', 'sibling', 'group', or 'self':
|
Chris@0
|
693 * - 'parent' will only look for fields up the tree.
|
Chris@0
|
694 * - 'sibling' will look for fields in the same group in rows above and
|
Chris@0
|
695 * below it.
|
Chris@0
|
696 * - 'self' affects the dragged row itself.
|
Chris@0
|
697 * - 'group' affects the dragged row, plus any children below it (the entire
|
Chris@0
|
698 * dragged group).
|
Chris@0
|
699 * - 'group': A class name applied on all related form elements for this action.
|
Chris@0
|
700 * - 'subgroup': (optional) If the group has several subgroups within it, this
|
Chris@0
|
701 * string should contain the class name identifying fields in the same
|
Chris@0
|
702 * subgroup.
|
Chris@0
|
703 * - 'source': (optional) If the $action is 'match', this string should contain
|
Chris@0
|
704 * the classname identifying what field will be used as the source value
|
Chris@0
|
705 * when matching the value in $subgroup.
|
Chris@0
|
706 * - 'hidden': (optional) The column containing the field elements may be
|
Chris@0
|
707 * entirely hidden from view dynamically when the JavaScript is loaded. Set
|
Chris@0
|
708 * to FALSE if the column should not be hidden.
|
Chris@0
|
709 * - 'limit': (optional) Limit the maximum amount of parenting in this table.
|
Chris@0
|
710 *
|
Chris@0
|
711 * @see MenuForm::BuildOverviewForm()
|
Chris@0
|
712 */
|
Chris@0
|
713 function drupal_attach_tabledrag(&$element, array $options) {
|
Chris@0
|
714 // Add default values to elements.
|
Chris@0
|
715 $options = $options + [
|
Chris@0
|
716 'subgroup' => NULL,
|
Chris@0
|
717 'source' => NULL,
|
Chris@0
|
718 'hidden' => TRUE,
|
Chris@0
|
719 'limit' => 0
|
Chris@0
|
720 ];
|
Chris@0
|
721
|
Chris@0
|
722 $group = $options['group'];
|
Chris@0
|
723
|
Chris@0
|
724 $tabledrag_id = &drupal_static(__FUNCTION__);
|
Chris@0
|
725 $tabledrag_id = (!isset($tabledrag_id)) ? 0 : $tabledrag_id + 1;
|
Chris@0
|
726
|
Chris@0
|
727 // If a subgroup or source isn't set, assume it is the same as the group.
|
Chris@0
|
728 $target = isset($options['subgroup']) ? $options['subgroup'] : $group;
|
Chris@0
|
729 $source = isset($options['source']) ? $options['source'] : $target;
|
Chris@0
|
730 $element['#attached']['drupalSettings']['tableDrag'][$options['table_id']][$group][$tabledrag_id] = [
|
Chris@0
|
731 'target' => $target,
|
Chris@0
|
732 'source' => $source,
|
Chris@0
|
733 'relationship' => $options['relationship'],
|
Chris@0
|
734 'action' => $options['action'],
|
Chris@0
|
735 'hidden' => $options['hidden'],
|
Chris@0
|
736 'limit' => $options['limit'],
|
Chris@0
|
737 ];
|
Chris@0
|
738
|
Chris@0
|
739 $element['#attached']['library'][] = 'core/drupal.tabledrag';
|
Chris@0
|
740 }
|
Chris@0
|
741
|
Chris@0
|
742 /**
|
Chris@0
|
743 * Deletes old cached JavaScript files and variables.
|
Chris@0
|
744 *
|
Chris@0
|
745 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
|
Chris@0
|
746 * Use \Drupal\Core\Asset\AssetCollectionOptimizerInterface::deleteAll().
|
Chris@12
|
747 *
|
Chris@12
|
748 * @see https://www.drupal.org/node/2317841
|
Chris@0
|
749 */
|
Chris@0
|
750 function drupal_clear_js_cache() {
|
Chris@0
|
751 \Drupal::service('asset.js.collection_optimizer')->deleteAll();
|
Chris@0
|
752 }
|
Chris@0
|
753
|
Chris@0
|
754 /**
|
Chris@0
|
755 * Pre-render callback: Renders a link into #markup.
|
Chris@0
|
756 *
|
Chris@0
|
757 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
|
Chris@0
|
758 * Use \Drupal\Core\Render\Element\Link::preRenderLink().
|
Chris@0
|
759 */
|
Chris@0
|
760 function drupal_pre_render_link($element) {
|
Chris@0
|
761 return Link::preRenderLink($element);
|
Chris@0
|
762 }
|
Chris@0
|
763
|
Chris@0
|
764 /**
|
Chris@0
|
765 * Pre-render callback: Collects child links into a single array.
|
Chris@0
|
766 *
|
Chris@0
|
767 * This function can be added as a pre_render callback for a renderable array,
|
Chris@0
|
768 * usually one which will be themed by links.html.twig. It iterates through all
|
Chris@0
|
769 * unrendered children of the element, collects any #links properties it finds,
|
Chris@0
|
770 * merges them into the parent element's #links array, and prevents those
|
Chris@0
|
771 * children from being rendered separately.
|
Chris@0
|
772 *
|
Chris@0
|
773 * The purpose of this is to allow links to be logically grouped into related
|
Chris@0
|
774 * categories, so that each child group can be rendered as its own list of
|
Chris@0
|
775 * links if drupal_render() is called on it, but calling drupal_render() on the
|
Chris@0
|
776 * parent element will still produce a single list containing all the remaining
|
Chris@0
|
777 * links, regardless of what group they were in.
|
Chris@0
|
778 *
|
Chris@0
|
779 * A typical example comes from node links, which are stored in a renderable
|
Chris@0
|
780 * array similar to this:
|
Chris@0
|
781 * @code
|
Chris@0
|
782 * $build['links'] = array(
|
Chris@0
|
783 * '#theme' => 'links__node',
|
Chris@0
|
784 * '#pre_render' => array('drupal_pre_render_links'),
|
Chris@0
|
785 * 'comment' => array(
|
Chris@0
|
786 * '#theme' => 'links__node__comment',
|
Chris@0
|
787 * '#links' => array(
|
Chris@0
|
788 * // An array of links associated with node comments, suitable for
|
Chris@0
|
789 * // passing in to links.html.twig.
|
Chris@0
|
790 * ),
|
Chris@0
|
791 * ),
|
Chris@0
|
792 * 'statistics' => array(
|
Chris@0
|
793 * '#theme' => 'links__node__statistics',
|
Chris@0
|
794 * '#links' => array(
|
Chris@0
|
795 * // An array of links associated with node statistics, suitable for
|
Chris@0
|
796 * // passing in to links.html.twig.
|
Chris@0
|
797 * ),
|
Chris@0
|
798 * ),
|
Chris@0
|
799 * 'translation' => array(
|
Chris@0
|
800 * '#theme' => 'links__node__translation',
|
Chris@0
|
801 * '#links' => array(
|
Chris@0
|
802 * // An array of links associated with node translation, suitable for
|
Chris@0
|
803 * // passing in to links.html.twig.
|
Chris@0
|
804 * ),
|
Chris@0
|
805 * ),
|
Chris@0
|
806 * );
|
Chris@0
|
807 * @endcode
|
Chris@0
|
808 *
|
Chris@0
|
809 * In this example, the links are grouped by functionality, which can be
|
Chris@0
|
810 * helpful to themers who want to display certain kinds of links independently.
|
Chris@0
|
811 * For example, adding this code to node.html.twig will result in the comment
|
Chris@0
|
812 * links being rendered as a single list:
|
Chris@0
|
813 * @code
|
Chris@0
|
814 * {{ content.links.comment }}
|
Chris@0
|
815 * @endcode
|
Chris@0
|
816 *
|
Chris@0
|
817 * (where a node's content has been transformed into $content before handing
|
Chris@0
|
818 * control to the node.html.twig template).
|
Chris@0
|
819 *
|
Chris@0
|
820 * The pre_render function defined here allows the above flexibility, but also
|
Chris@0
|
821 * allows the following code to be used to render all remaining links into a
|
Chris@0
|
822 * single list, regardless of their group:
|
Chris@0
|
823 * @code
|
Chris@0
|
824 * {{ content.links }}
|
Chris@0
|
825 * @endcode
|
Chris@0
|
826 *
|
Chris@0
|
827 * In the above example, this will result in the statistics and translation
|
Chris@0
|
828 * links being rendered together in a single list (but not the comment links,
|
Chris@0
|
829 * which were rendered previously on their own).
|
Chris@0
|
830 *
|
Chris@0
|
831 * Because of the way this function works, the individual properties of each
|
Chris@0
|
832 * group (for example, a group-specific #theme property such as
|
Chris@0
|
833 * 'links__node__comment' in the example above, or any other property such as
|
Chris@0
|
834 * #attributes or #pre_render that is attached to it) are only used when that
|
Chris@0
|
835 * group is rendered on its own. When the group is rendered together with other
|
Chris@0
|
836 * children, these child-specific properties are ignored, and only the overall
|
Chris@0
|
837 * properties of the parent are used.
|
Chris@0
|
838 */
|
Chris@0
|
839 function drupal_pre_render_links($element) {
|
Chris@0
|
840 $element += ['#links' => [], '#attached' => []];
|
Chris@0
|
841 foreach (Element::children($element) as $key) {
|
Chris@0
|
842 $child = &$element[$key];
|
Chris@0
|
843 // If the child has links which have not been printed yet and the user has
|
Chris@0
|
844 // access to it, merge its links in to the parent.
|
Chris@0
|
845 if (isset($child['#links']) && empty($child['#printed']) && Element::isVisibleElement($child)) {
|
Chris@0
|
846 $element['#links'] += $child['#links'];
|
Chris@0
|
847 // Mark the child as having been printed already (so that its links
|
Chris@0
|
848 // cannot be mistakenly rendered twice).
|
Chris@0
|
849 $child['#printed'] = TRUE;
|
Chris@0
|
850 }
|
Chris@0
|
851 // Merge attachments.
|
Chris@0
|
852 if (isset($child['#attached'])) {
|
Chris@0
|
853 $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], $child['#attached']);
|
Chris@0
|
854 }
|
Chris@0
|
855 }
|
Chris@0
|
856 return $element;
|
Chris@0
|
857 }
|
Chris@0
|
858
|
Chris@0
|
859 /**
|
Chris@0
|
860 * Renders final HTML given a structured array tree.
|
Chris@0
|
861 *
|
Chris@0
|
862 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use the
|
Chris@0
|
863 * 'renderer' service instead.
|
Chris@0
|
864 *
|
Chris@0
|
865 * @see \Drupal\Core\Render\RendererInterface::renderRoot()
|
Chris@12
|
866 * @see https://www.drupal.org/node/2912696
|
Chris@0
|
867 */
|
Chris@0
|
868 function drupal_render_root(&$elements) {
|
Chris@0
|
869 return \Drupal::service('renderer')->renderRoot($elements);
|
Chris@0
|
870 }
|
Chris@0
|
871
|
Chris@0
|
872 /**
|
Chris@0
|
873 * Renders HTML given a structured array tree.
|
Chris@0
|
874 *
|
Chris@0
|
875 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use the
|
Chris@0
|
876 * 'renderer' service instead.
|
Chris@0
|
877 *
|
Chris@0
|
878 * @see \Drupal\Core\Render\RendererInterface::render()
|
Chris@12
|
879 * @see https://www.drupal.org/node/2912696
|
Chris@0
|
880 */
|
Chris@0
|
881 function drupal_render(&$elements, $is_recursive_call = FALSE) {
|
Chris@0
|
882 return \Drupal::service('renderer')->render($elements, $is_recursive_call);
|
Chris@0
|
883 }
|
Chris@0
|
884
|
Chris@0
|
885 /**
|
Chris@0
|
886 * Renders children of an element and concatenates them.
|
Chris@0
|
887 *
|
Chris@0
|
888 * @param array $element
|
Chris@0
|
889 * The structured array whose children shall be rendered.
|
Chris@0
|
890 * @param array $children_keys
|
Chris@0
|
891 * (optional) If the keys of the element's children are already known, they
|
Chris@0
|
892 * can be passed in to save another run of
|
Chris@0
|
893 * \Drupal\Core\Render\Element::children().
|
Chris@0
|
894 *
|
Chris@0
|
895 * @return string|\Drupal\Component\Render\MarkupInterface
|
Chris@0
|
896 * The rendered HTML of all children of the element.
|
Chris@0
|
897 *
|
Chris@0
|
898 * @deprecated in Drupal 8.0.x and will be removed before 9.0.0. Avoid early
|
Chris@0
|
899 * rendering when possible or loop through the elements and render them as
|
Chris@0
|
900 * they are available.
|
Chris@0
|
901 *
|
Chris@16
|
902 * @see \Drupal\Core\Render\RendererInterface::render()
|
Chris@12
|
903 * @see https://www.drupal.org/node/2912757
|
Chris@0
|
904 */
|
Chris@0
|
905 function drupal_render_children(&$element, $children_keys = NULL) {
|
Chris@0
|
906 if ($children_keys === NULL) {
|
Chris@0
|
907 $children_keys = Element::children($element);
|
Chris@0
|
908 }
|
Chris@0
|
909 $output = '';
|
Chris@0
|
910 foreach ($children_keys as $key) {
|
Chris@0
|
911 if (!empty($element[$key])) {
|
Chris@0
|
912 $output .= \Drupal::service('renderer')->render($element[$key]);
|
Chris@0
|
913 }
|
Chris@0
|
914 }
|
Chris@0
|
915 return Markup::create($output);
|
Chris@0
|
916 }
|
Chris@0
|
917
|
Chris@0
|
918 /**
|
Chris@0
|
919 * Renders an element.
|
Chris@0
|
920 *
|
Chris@0
|
921 * This function renders an element. The top level element is shown with show()
|
Chris@0
|
922 * before rendering, so it will always be rendered even if hide() had been
|
Chris@0
|
923 * previously used on it.
|
Chris@0
|
924 *
|
Chris@0
|
925 * @param $element
|
Chris@0
|
926 * The element to be rendered.
|
Chris@0
|
927 *
|
Chris@0
|
928 * @return
|
Chris@0
|
929 * The rendered element.
|
Chris@0
|
930 *
|
Chris@0
|
931 * @see \Drupal\Core\Render\RendererInterface
|
Chris@0
|
932 * @see show()
|
Chris@0
|
933 * @see hide()
|
Chris@0
|
934 */
|
Chris@0
|
935 function render(&$element) {
|
Chris@0
|
936 if (!$element && $element !== 0) {
|
Chris@0
|
937 return NULL;
|
Chris@0
|
938 }
|
Chris@0
|
939 if (is_array($element)) {
|
Chris@0
|
940 // Early return if this element was pre-rendered (no need to re-render).
|
Chris@0
|
941 if (isset($element['#printed']) && $element['#printed'] == TRUE && isset($element['#markup']) && strlen($element['#markup']) > 0) {
|
Chris@0
|
942 return $element['#markup'];
|
Chris@0
|
943 }
|
Chris@0
|
944 show($element);
|
Chris@0
|
945 return \Drupal::service('renderer')->render($element);
|
Chris@0
|
946 }
|
Chris@0
|
947 else {
|
Chris@0
|
948 // Safe-guard for inappropriate use of render() on flat variables: return
|
Chris@0
|
949 // the variable as-is.
|
Chris@0
|
950 return $element;
|
Chris@0
|
951 }
|
Chris@0
|
952 }
|
Chris@0
|
953
|
Chris@0
|
954 /**
|
Chris@0
|
955 * Hides an element from later rendering.
|
Chris@0
|
956 *
|
Chris@0
|
957 * The first time render() or drupal_render() is called on an element tree,
|
Chris@0
|
958 * as each element in the tree is rendered, it is marked with a #printed flag
|
Chris@0
|
959 * and the rendered children of the element are cached. Subsequent calls to
|
Chris@0
|
960 * render() or drupal_render() will not traverse the child tree of this element
|
Chris@0
|
961 * again: they will just use the cached children. So if you want to hide an
|
Chris@0
|
962 * element, be sure to call hide() on the element before its parent tree is
|
Chris@0
|
963 * rendered for the first time, as it will have no effect on subsequent
|
Chris@0
|
964 * renderings of the parent tree.
|
Chris@0
|
965 *
|
Chris@0
|
966 * @param $element
|
Chris@0
|
967 * The element to be hidden.
|
Chris@0
|
968 *
|
Chris@0
|
969 * @return
|
Chris@0
|
970 * The element.
|
Chris@0
|
971 *
|
Chris@0
|
972 * @see render()
|
Chris@0
|
973 * @see show()
|
Chris@0
|
974 */
|
Chris@0
|
975 function hide(&$element) {
|
Chris@0
|
976 $element['#printed'] = TRUE;
|
Chris@0
|
977 return $element;
|
Chris@0
|
978 }
|
Chris@0
|
979
|
Chris@0
|
980 /**
|
Chris@0
|
981 * Shows a hidden element for later rendering.
|
Chris@0
|
982 *
|
Chris@0
|
983 * You can also use render($element), which shows the element while rendering
|
Chris@0
|
984 * it.
|
Chris@0
|
985 *
|
Chris@0
|
986 * The first time render() or drupal_render() is called on an element tree,
|
Chris@0
|
987 * as each element in the tree is rendered, it is marked with a #printed flag
|
Chris@0
|
988 * and the rendered children of the element are cached. Subsequent calls to
|
Chris@0
|
989 * render() or drupal_render() will not traverse the child tree of this element
|
Chris@0
|
990 * again: they will just use the cached children. So if you want to show an
|
Chris@0
|
991 * element, be sure to call show() on the element before its parent tree is
|
Chris@0
|
992 * rendered for the first time, as it will have no effect on subsequent
|
Chris@0
|
993 * renderings of the parent tree.
|
Chris@0
|
994 *
|
Chris@0
|
995 * @param $element
|
Chris@0
|
996 * The element to be shown.
|
Chris@0
|
997 *
|
Chris@0
|
998 * @return
|
Chris@0
|
999 * The element.
|
Chris@0
|
1000 *
|
Chris@0
|
1001 * @see render()
|
Chris@0
|
1002 * @see hide()
|
Chris@0
|
1003 */
|
Chris@0
|
1004 function show(&$element) {
|
Chris@0
|
1005 $element['#printed'] = FALSE;
|
Chris@0
|
1006 return $element;
|
Chris@0
|
1007 }
|
Chris@0
|
1008
|
Chris@0
|
1009 /**
|
Chris@0
|
1010 * Retrieves the default properties for the defined element type.
|
Chris@0
|
1011 *
|
Chris@0
|
1012 * @param $type
|
Chris@0
|
1013 * An element type as defined by an element plugin.
|
Chris@0
|
1014 *
|
Chris@0
|
1015 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
1016 * Use \Drupal::service('element_info')->getInfo() instead.
|
Chris@12
|
1017 *
|
Chris@12
|
1018 * @see https://www.drupal.org/node/2235461
|
Chris@0
|
1019 */
|
Chris@0
|
1020 function element_info($type) {
|
Chris@0
|
1021 return \Drupal::service('element_info')->getInfo($type);
|
Chris@0
|
1022 }
|
Chris@0
|
1023
|
Chris@0
|
1024 /**
|
Chris@0
|
1025 * Retrieves a single property for the defined element type.
|
Chris@0
|
1026 *
|
Chris@0
|
1027 * @param $type
|
Chris@0
|
1028 * An element type as defined by an element plugin.
|
Chris@0
|
1029 * @param $property_name
|
Chris@0
|
1030 * The property within the element type that should be returned.
|
Chris@0
|
1031 * @param $default
|
Chris@0
|
1032 * (Optional) The value to return if the element type does not specify a
|
Chris@0
|
1033 * value for the property. Defaults to NULL.
|
Chris@0
|
1034 *
|
Chris@0
|
1035 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
1036 * Use \Drupal::service('element_info')->getInfoProperty() instead.
|
Chris@12
|
1037 *
|
Chris@12
|
1038 * @see https://www.drupal.org/node/2235461
|
Chris@0
|
1039 */
|
Chris@0
|
1040 function element_info_property($type, $property_name, $default = NULL) {
|
Chris@0
|
1041 return \Drupal::service('element_info')->getInfoProperty($type, $property_name, $default);
|
Chris@0
|
1042 }
|
Chris@0
|
1043
|
Chris@0
|
1044 /**
|
Chris@0
|
1045 * Flushes all persistent caches, resets all variables, and rebuilds all data structures.
|
Chris@0
|
1046 *
|
Chris@0
|
1047 * At times, it is necessary to re-initialize the entire system to account for
|
Chris@0
|
1048 * changed or new code. This function:
|
Chris@0
|
1049 * - Clears all persistent caches:
|
Chris@0
|
1050 * - The bootstrap cache bin containing base system, module system, and theme
|
Chris@0
|
1051 * system information.
|
Chris@0
|
1052 * - The common 'default' cache bin containing arbitrary caches.
|
Chris@0
|
1053 * - The page cache.
|
Chris@0
|
1054 * - The URL alias path cache.
|
Chris@0
|
1055 * - Resets all static variables that have been defined via drupal_static().
|
Chris@0
|
1056 * - Clears asset (JS/CSS) file caches.
|
Chris@0
|
1057 * - Updates the system with latest information about extensions (modules and
|
Chris@0
|
1058 * themes).
|
Chris@0
|
1059 * - Updates the bootstrap flag for modules implementing bootstrap_hooks().
|
Chris@0
|
1060 * - Rebuilds the full database schema information (invoking hook_schema()).
|
Chris@0
|
1061 * - Rebuilds data structures of all modules (invoking hook_rebuild()). In
|
Chris@0
|
1062 * core this means
|
Chris@0
|
1063 * - blocks, node types, date formats and actions are synchronized with the
|
Chris@0
|
1064 * database
|
Chris@0
|
1065 * - The 'active' status of fields is refreshed.
|
Chris@0
|
1066 * - Rebuilds the menu router.
|
Chris@0
|
1067 *
|
Chris@0
|
1068 * This means the entire system is reset so all caches and static variables are
|
Chris@0
|
1069 * effectively empty. After that is guaranteed, information about the currently
|
Chris@0
|
1070 * active code is updated, and rebuild operations are successively called in
|
Chris@0
|
1071 * order to synchronize the active system according to the current information
|
Chris@0
|
1072 * defined in code.
|
Chris@0
|
1073 *
|
Chris@0
|
1074 * All modules need to ensure that all of their caches are flushed when
|
Chris@0
|
1075 * hook_cache_flush() is invoked; any previously known information must no
|
Chris@0
|
1076 * longer exist. All following hook_rebuild() operations must be based on fresh
|
Chris@0
|
1077 * and current system data. All modules must be able to rely on this contract.
|
Chris@0
|
1078 *
|
Chris@0
|
1079 * @see \Drupal\Core\Cache\CacheHelper::getBins()
|
Chris@0
|
1080 * @see hook_cache_flush()
|
Chris@0
|
1081 * @see hook_rebuild()
|
Chris@0
|
1082 *
|
Chris@0
|
1083 * This function also resets the theme, which means it is not initialized
|
Chris@0
|
1084 * anymore and all previously added JavaScript and CSS is gone. Normally, this
|
Chris@0
|
1085 * function is called as an end-of-POST-request operation that is followed by a
|
Chris@0
|
1086 * redirect, so this effect is not visible. Since the full reset is the whole
|
Chris@0
|
1087 * point of this function, callers need to take care for backing up all needed
|
Chris@0
|
1088 * variables and properly restoring or re-initializing them on their own. For
|
Chris@0
|
1089 * convenience, this function automatically re-initializes the maintenance theme
|
Chris@0
|
1090 * if it was initialized before.
|
Chris@0
|
1091 *
|
Chris@0
|
1092 * @todo Try to clear page/JS/CSS caches last, so cached pages can still be
|
Chris@0
|
1093 * served during this possibly long-running operation. (Conflict on bootstrap
|
Chris@0
|
1094 * cache though.)
|
Chris@0
|
1095 * @todo Add a global lock to ensure that caches are not primed in concurrent
|
Chris@0
|
1096 * requests.
|
Chris@0
|
1097 */
|
Chris@0
|
1098 function drupal_flush_all_caches() {
|
Chris@0
|
1099 $module_handler = \Drupal::moduleHandler();
|
Chris@0
|
1100 // Flush all persistent caches.
|
Chris@0
|
1101 // This is executed based on old/previously known information, which is
|
Chris@0
|
1102 // sufficient, since new extensions cannot have any primed caches yet.
|
Chris@0
|
1103 $module_handler->invokeAll('cache_flush');
|
Chris@0
|
1104 foreach (Cache::getBins() as $service_id => $cache_backend) {
|
Chris@0
|
1105 $cache_backend->deleteAll();
|
Chris@0
|
1106 }
|
Chris@0
|
1107
|
Chris@0
|
1108 // Flush asset file caches.
|
Chris@0
|
1109 \Drupal::service('asset.css.collection_optimizer')->deleteAll();
|
Chris@0
|
1110 \Drupal::service('asset.js.collection_optimizer')->deleteAll();
|
Chris@0
|
1111 _drupal_flush_css_js();
|
Chris@0
|
1112
|
Chris@0
|
1113 // Reset all static caches.
|
Chris@0
|
1114 drupal_static_reset();
|
Chris@0
|
1115
|
Chris@0
|
1116 // Invalidate the container.
|
Chris@0
|
1117 \Drupal::service('kernel')->invalidateContainer();
|
Chris@0
|
1118
|
Chris@0
|
1119 // Wipe the Twig PHP Storage cache.
|
Chris@0
|
1120 PhpStorageFactory::get('twig')->deleteAll();
|
Chris@0
|
1121
|
Chris@0
|
1122 // Rebuild module and theme data.
|
Chris@0
|
1123 $module_data = system_rebuild_module_data();
|
Chris@0
|
1124 /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
|
Chris@0
|
1125 $theme_handler = \Drupal::service('theme_handler');
|
Chris@0
|
1126 $theme_handler->refreshInfo();
|
Chris@0
|
1127 // In case the active theme gets requested later in the same request we need
|
Chris@0
|
1128 // to reset the theme manager.
|
Chris@0
|
1129 \Drupal::theme()->resetActiveTheme();
|
Chris@0
|
1130
|
Chris@0
|
1131 // Rebuild and reboot a new kernel. A simple DrupalKernel reboot is not
|
Chris@0
|
1132 // sufficient, since the list of enabled modules might have been adjusted
|
Chris@0
|
1133 // above due to changed code.
|
Chris@0
|
1134 $files = [];
|
Chris@0
|
1135 foreach ($module_data as $name => $extension) {
|
Chris@0
|
1136 if ($extension->status) {
|
Chris@0
|
1137 $files[$name] = $extension;
|
Chris@0
|
1138 }
|
Chris@0
|
1139 }
|
Chris@0
|
1140 \Drupal::service('kernel')->updateModules($module_handler->getModuleList(), $files);
|
Chris@0
|
1141 // New container, new module handler.
|
Chris@0
|
1142 $module_handler = \Drupal::moduleHandler();
|
Chris@0
|
1143
|
Chris@0
|
1144 // Ensure that all modules that are currently supposed to be enabled are
|
Chris@0
|
1145 // actually loaded.
|
Chris@0
|
1146 $module_handler->loadAll();
|
Chris@0
|
1147
|
Chris@0
|
1148 // Rebuild all information based on new module data.
|
Chris@0
|
1149 $module_handler->invokeAll('rebuild');
|
Chris@0
|
1150
|
Chris@0
|
1151 // Clear all plugin caches.
|
Chris@0
|
1152 \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
|
Chris@0
|
1153
|
Chris@0
|
1154 // Rebuild the menu router based on all rebuilt data.
|
Chris@0
|
1155 // Important: This rebuild must happen last, so the menu router is guaranteed
|
Chris@0
|
1156 // to be based on up to date information.
|
Chris@0
|
1157 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
1158
|
Chris@0
|
1159 // Re-initialize the maintenance theme, if the current request attempted to
|
Chris@0
|
1160 // use it. Unlike regular usages of this function, the installer and update
|
Chris@0
|
1161 // scripts need to flush all caches during GET requests/page building.
|
Chris@0
|
1162 if (function_exists('_drupal_maintenance_theme')) {
|
Chris@0
|
1163 \Drupal::theme()->resetActiveTheme();
|
Chris@0
|
1164 drupal_maintenance_theme();
|
Chris@0
|
1165 }
|
Chris@0
|
1166 }
|
Chris@0
|
1167
|
Chris@0
|
1168 /**
|
Chris@0
|
1169 * Changes the dummy query string added to all CSS and JavaScript files.
|
Chris@0
|
1170 *
|
Chris@0
|
1171 * Changing the dummy query string appended to CSS and JavaScript files forces
|
Chris@0
|
1172 * all browsers to reload fresh files.
|
Chris@0
|
1173 */
|
Chris@0
|
1174 function _drupal_flush_css_js() {
|
Chris@0
|
1175 // The timestamp is converted to base 36 in order to make it more compact.
|
Chris@0
|
1176 Drupal::state()->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));
|
Chris@0
|
1177 }
|
Chris@0
|
1178
|
Chris@0
|
1179 /**
|
Chris@0
|
1180 * Outputs debug information.
|
Chris@0
|
1181 *
|
Chris@0
|
1182 * The debug information is passed on to trigger_error() after being converted
|
Chris@0
|
1183 * to a string using _drupal_debug_message().
|
Chris@0
|
1184 *
|
Chris@0
|
1185 * @param $data
|
Chris@0
|
1186 * Data to be output.
|
Chris@0
|
1187 * @param $label
|
Chris@0
|
1188 * Label to prefix the data.
|
Chris@0
|
1189 * @param $print_r
|
Chris@0
|
1190 * Flag to switch between print_r() and var_export() for data conversion to
|
Chris@0
|
1191 * string. Set $print_r to FALSE to use var_export() instead of print_r().
|
Chris@0
|
1192 * Passing recursive data structures to var_export() will generate an error.
|
Chris@0
|
1193 */
|
Chris@0
|
1194 function debug($data, $label = NULL, $print_r = TRUE) {
|
Chris@0
|
1195 // Print $data contents to string.
|
Chris@0
|
1196 $string = Html::escape($print_r ? print_r($data, TRUE) : var_export($data, TRUE));
|
Chris@0
|
1197
|
Chris@0
|
1198 // Display values with pre-formatting to increase readability.
|
Chris@0
|
1199 $string = '<pre>' . $string . '</pre>';
|
Chris@0
|
1200
|
Chris@0
|
1201 trigger_error(trim($label ? "$label: $string" : $string));
|
Chris@0
|
1202 }
|
Chris@0
|
1203
|
Chris@0
|
1204 /**
|
Chris@0
|
1205 * Checks whether a version is compatible with a given dependency.
|
Chris@0
|
1206 *
|
Chris@0
|
1207 * @param $v
|
Chris@0
|
1208 * A parsed dependency structure e.g. from ModuleHandler::parseDependency().
|
Chris@0
|
1209 * @param $current_version
|
Chris@0
|
1210 * The version to check against (like 4.2).
|
Chris@0
|
1211 *
|
Chris@0
|
1212 * @return
|
Chris@0
|
1213 * NULL if compatible, otherwise the original dependency version string that
|
Chris@0
|
1214 * caused the incompatibility.
|
Chris@0
|
1215 *
|
Chris@0
|
1216 * @see \Drupal\Core\Extension\ModuleHandler::parseDependency()
|
Chris@0
|
1217 */
|
Chris@0
|
1218 function drupal_check_incompatibility($v, $current_version) {
|
Chris@0
|
1219 if (!empty($v['versions'])) {
|
Chris@0
|
1220 foreach ($v['versions'] as $required_version) {
|
Chris@0
|
1221 if ((isset($required_version['op']) && !version_compare($current_version, $required_version['version'], $required_version['op']))) {
|
Chris@0
|
1222 return $v['original_version'];
|
Chris@0
|
1223 }
|
Chris@0
|
1224 }
|
Chris@0
|
1225 }
|
Chris@0
|
1226 }
|
Chris@0
|
1227
|
Chris@0
|
1228 /**
|
Chris@0
|
1229 * Returns a string of supported archive extensions.
|
Chris@0
|
1230 *
|
Chris@0
|
1231 * @return
|
Chris@0
|
1232 * A space-separated string of extensions suitable for use by the file
|
Chris@0
|
1233 * validation system.
|
Chris@0
|
1234 */
|
Chris@0
|
1235 function archiver_get_extensions() {
|
Chris@0
|
1236 $valid_extensions = [];
|
Chris@0
|
1237 foreach (\Drupal::service('plugin.manager.archiver')->getDefinitions() as $archive) {
|
Chris@0
|
1238 foreach ($archive['extensions'] as $extension) {
|
Chris@0
|
1239 foreach (explode('.', $extension) as $part) {
|
Chris@0
|
1240 if (!in_array($part, $valid_extensions)) {
|
Chris@0
|
1241 $valid_extensions[] = $part;
|
Chris@0
|
1242 }
|
Chris@0
|
1243 }
|
Chris@0
|
1244 }
|
Chris@0
|
1245 }
|
Chris@0
|
1246 return implode(' ', $valid_extensions);
|
Chris@0
|
1247 }
|
Chris@0
|
1248
|
Chris@0
|
1249 /**
|
Chris@0
|
1250 * Creates the appropriate archiver for the specified file.
|
Chris@0
|
1251 *
|
Chris@0
|
1252 * @param $file
|
Chris@0
|
1253 * The full path of the archive file. Note that stream wrapper paths are
|
Chris@0
|
1254 * supported, but not remote ones.
|
Chris@0
|
1255 *
|
Chris@0
|
1256 * @return
|
Chris@0
|
1257 * A newly created instance of the archiver class appropriate
|
Chris@0
|
1258 * for the specified file, already bound to that file.
|
Chris@0
|
1259 * If no appropriate archiver class was found, will return FALSE.
|
Chris@0
|
1260 */
|
Chris@0
|
1261 function archiver_get_archiver($file) {
|
Chris@0
|
1262 // Archivers can only work on local paths
|
Chris@14
|
1263 $filepath = \Drupal::service('file_system')->realpath($file);
|
Chris@0
|
1264 if (!is_file($filepath)) {
|
Chris@0
|
1265 throw new Exception(t('Archivers can only operate on local files: %file not supported', ['%file' => $file]));
|
Chris@0
|
1266 }
|
Chris@0
|
1267 return \Drupal::service('plugin.manager.archiver')->getInstance(['filepath' => $filepath]);
|
Chris@0
|
1268 }
|
Chris@0
|
1269
|
Chris@0
|
1270 /**
|
Chris@0
|
1271 * Assembles the Drupal Updater registry.
|
Chris@0
|
1272 *
|
Chris@0
|
1273 * An Updater is a class that knows how to update various parts of the Drupal
|
Chris@0
|
1274 * file system, for example to update modules that have newer releases, or to
|
Chris@0
|
1275 * install a new theme.
|
Chris@0
|
1276 *
|
Chris@0
|
1277 * @return array
|
Chris@0
|
1278 * The Drupal Updater class registry.
|
Chris@0
|
1279 *
|
Chris@0
|
1280 * @see \Drupal\Core\Updater\Updater
|
Chris@0
|
1281 * @see hook_updater_info()
|
Chris@0
|
1282 * @see hook_updater_info_alter()
|
Chris@0
|
1283 */
|
Chris@0
|
1284 function drupal_get_updaters() {
|
Chris@0
|
1285 $updaters = &drupal_static(__FUNCTION__);
|
Chris@0
|
1286 if (!isset($updaters)) {
|
Chris@0
|
1287 $updaters = \Drupal::moduleHandler()->invokeAll('updater_info');
|
Chris@0
|
1288 \Drupal::moduleHandler()->alter('updater_info', $updaters);
|
Chris@0
|
1289 uasort($updaters, [SortArray::class, 'sortByWeightElement']);
|
Chris@0
|
1290 }
|
Chris@0
|
1291 return $updaters;
|
Chris@0
|
1292 }
|
Chris@0
|
1293
|
Chris@0
|
1294 /**
|
Chris@0
|
1295 * Assembles the Drupal FileTransfer registry.
|
Chris@0
|
1296 *
|
Chris@0
|
1297 * @return
|
Chris@0
|
1298 * The Drupal FileTransfer class registry.
|
Chris@0
|
1299 *
|
Chris@0
|
1300 * @see \Drupal\Core\FileTransfer\FileTransfer
|
Chris@0
|
1301 * @see hook_filetransfer_info()
|
Chris@0
|
1302 * @see hook_filetransfer_info_alter()
|
Chris@0
|
1303 */
|
Chris@0
|
1304 function drupal_get_filetransfer_info() {
|
Chris@0
|
1305 $info = &drupal_static(__FUNCTION__);
|
Chris@0
|
1306 if (!isset($info)) {
|
Chris@0
|
1307 $info = \Drupal::moduleHandler()->invokeAll('filetransfer_info');
|
Chris@0
|
1308 \Drupal::moduleHandler()->alter('filetransfer_info', $info);
|
Chris@0
|
1309 uasort($info, [SortArray::class, 'sortByWeightElement']);
|
Chris@0
|
1310 }
|
Chris@0
|
1311 return $info;
|
Chris@0
|
1312 }
|