comparison core/includes/common.inc @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
15 use Drupal\Component\Utility\UrlHelper; 15 use Drupal\Component\Utility\UrlHelper;
16 use Drupal\Core\Cache\Cache; 16 use Drupal\Core\Cache\Cache;
17 use Drupal\Core\Render\Element\Link; 17 use Drupal\Core\Render\Element\Link;
18 use Drupal\Core\Render\Markup; 18 use Drupal\Core\Render\Markup;
19 use Drupal\Core\StringTranslation\TranslatableMarkup; 19 use Drupal\Core\StringTranslation\TranslatableMarkup;
20 use Drupal\Core\PhpStorage\PhpStorageFactory;
21 use Drupal\Core\StringTranslation\PluralTranslatableMarkup; 20 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
22 use Drupal\Core\Render\BubbleableMetadata; 21 use Drupal\Core\Render\BubbleableMetadata;
23 use Drupal\Core\Render\Element; 22 use Drupal\Core\Render\Element;
24 23
25 /** 24 /**
45 * $my_substring = substr($original_string, 0, 5); 44 * $my_substring = substr($original_string, 0, 5);
46 * @endcode 45 * @endcode
47 * 46 *
48 * Correct: 47 * Correct:
49 * @code 48 * @code
50 * $my_substring = Unicode::substr($original_string, 0, 5); 49 * $my_substring = mb_substr($original_string, 0, 5);
51 * @endcode 50 * @endcode
52 * 51 *
53 * @} 52 * @}
54 */ 53 */
55 54
213 * \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() instead. 212 * \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() instead.
214 * 213 *
215 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. 214 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
216 * Use UrlHelper::stripDangerousProtocols() or UrlHelper::filterBadProtocol() 215 * Use UrlHelper::stripDangerousProtocols() or UrlHelper::filterBadProtocol()
217 * instead. UrlHelper::stripDangerousProtocols() can be used in conjunction 216 * instead. UrlHelper::stripDangerousProtocols() can be used in conjunction
218 * with \Drupal\Component\Utility\SafeMarkup::format() and an @variable 217 * with \Drupal\Component\Render\FormattableMarkup and an @variable
219 * placeholder which will perform the necessary escaping. 218 * placeholder which will perform the necessary escaping.
220 * UrlHelper::filterBadProtocol() is functionality equivalent to check_url() 219 * UrlHelper::filterBadProtocol() is functionality equivalent to check_url()
221 * apart from the fact it is protected from double escaping bugs. Note that 220 * apart from the fact it is protected from double escaping bugs. Note that
222 * this method no longer marks its output as safe. 221 * this method no longer marks its output as safe.
223 * 222 *
250 * 249 *
251 * @return \Drupal\Core\StringTranslation\TranslatableMarkup 250 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
252 * A translated string representation of the size. 251 * A translated string representation of the size.
253 */ 252 */
254 function format_size($size, $langcode = NULL) { 253 function format_size($size, $langcode = NULL) {
255 if ($size < Bytes::KILOBYTE) { 254 $absolute_size = abs($size);
255 if ($absolute_size < Bytes::KILOBYTE) {
256 return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]); 256 return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]);
257 } 257 }
258 else { 258 // Create a multiplier to preserve the sign of $size.
259 // Convert bytes to kilobytes. 259 $sign = $absolute_size / $size;
260 $size = $size / Bytes::KILOBYTE; 260 foreach (['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) {
261 $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; 261 $absolute_size /= Bytes::KILOBYTE;
262 foreach ($units as $unit) { 262 $rounded_size = round($absolute_size, 2);
263 if (round($size, 2) >= Bytes::KILOBYTE) { 263 if ($rounded_size < Bytes::KILOBYTE) {
264 $size = $size / Bytes::KILOBYTE; 264 break;
265 }
266 else {
267 break;
268 }
269 } 265 }
270 $args = ['@size' => round($size, 2)]; 266 }
271 $options = ['langcode' => $langcode]; 267 $args = ['@size' => $rounded_size * $sign];
272 switch ($unit) { 268 $options = ['langcode' => $langcode];
273 case 'KB': 269 switch ($unit) {
274 return new TranslatableMarkup('@size KB', $args, $options); 270 case 'KB':
275 case 'MB': 271 return new TranslatableMarkup('@size KB', $args, $options);
276 return new TranslatableMarkup('@size MB', $args, $options); 272
277 case 'GB': 273 case 'MB':
278 return new TranslatableMarkup('@size GB', $args, $options); 274 return new TranslatableMarkup('@size MB', $args, $options);
279 case 'TB': 275
280 return new TranslatableMarkup('@size TB', $args, $options); 276 case 'GB':
281 case 'PB': 277 return new TranslatableMarkup('@size GB', $args, $options);
282 return new TranslatableMarkup('@size PB', $args, $options); 278
283 case 'EB': 279 case 'TB':
284 return new TranslatableMarkup('@size EB', $args, $options); 280 return new TranslatableMarkup('@size TB', $args, $options);
285 case 'ZB': 281
286 return new TranslatableMarkup('@size ZB', $args, $options); 282 case 'PB':
287 case 'YB': 283 return new TranslatableMarkup('@size PB', $args, $options);
288 return new TranslatableMarkup('@size YB', $args, $options); 284
289 } 285 case 'EB':
286 return new TranslatableMarkup('@size EB', $args, $options);
287
288 case 'ZB':
289 return new TranslatableMarkup('@size ZB', $args, $options);
290
291 case 'YB':
292 return new TranslatableMarkup('@size YB', $args, $options);
290 } 293 }
291 } 294 }
292 295
293 /** 296 /**
294 * Formats a date, using a date type or a custom date format string. 297 * Formats a date, using a date type or a custom date format string.
407 } 410 }
408 411
409 /** 412 /**
410 * Returns the base URL path (i.e., directory) of the Drupal installation. 413 * Returns the base URL path (i.e., directory) of the Drupal installation.
411 * 414 *
412 * base_path() adds a "/" to the beginning and end of the returned path if the 415 * Function base_path() adds a "/" to the beginning and end of the returned path
413 * path is not empty. At the very least, this will return "/". 416 * if the path is not empty. At the very least, this will return "/".
414 * 417 *
415 * Examples: 418 * Examples:
416 * - http://example.com returns "/" because the path is empty. 419 * - http://example.com returns "/" because the path is empty.
417 * - http://example.com/drupal/folder returns "/drupal/folder/". 420 * - http://example.com/drupal/folder returns "/drupal/folder/".
418 */ 421 */
489 * ) 492 * )
490 * @endcode 493 * @endcode
491 * 494 *
492 * Every condition is a key/value pair, whose key is a jQuery selector that 495 * Every condition is a key/value pair, whose key is a jQuery selector that
493 * denotes another element on the page, and whose value is an array of 496 * denotes another element on the page, and whose value is an array of
494 * conditions, which must bet met on that element: 497 * conditions, which must be met on that element:
495 * @code 498 * @code
496 * array( 499 * array(
497 * 'visible' => array( 500 * 'visible' => array(
498 * JQUERY_SELECTOR => REMOTE_CONDITIONS, 501 * JQUERY_SELECTOR => REMOTE_CONDITIONS,
499 * JQUERY_SELECTOR => REMOTE_CONDITIONS, 502 * JQUERY_SELECTOR => REMOTE_CONDITIONS,
714 // Add default values to elements. 717 // Add default values to elements.
715 $options = $options + [ 718 $options = $options + [
716 'subgroup' => NULL, 719 'subgroup' => NULL,
717 'source' => NULL, 720 'source' => NULL,
718 'hidden' => TRUE, 721 'hidden' => TRUE,
719 'limit' => 0 722 'limit' => 0,
720 ]; 723 ];
721 724
722 $group = $options['group']; 725 $group = $options['group'];
723 726
724 $tabledrag_id = &drupal_static(__FUNCTION__); 727 $tabledrag_id = &drupal_static(__FUNCTION__);
1115 1118
1116 // Invalidate the container. 1119 // Invalidate the container.
1117 \Drupal::service('kernel')->invalidateContainer(); 1120 \Drupal::service('kernel')->invalidateContainer();
1118 1121
1119 // Wipe the Twig PHP Storage cache. 1122 // Wipe the Twig PHP Storage cache.
1120 PhpStorageFactory::get('twig')->deleteAll(); 1123 \Drupal::service('twig')->invalidate();
1121 1124
1122 // Rebuild module and theme data. 1125 // Rebuild module and theme data.
1123 $module_data = system_rebuild_module_data(); 1126 $module_data = system_rebuild_module_data();
1124 /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */ 1127 /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
1125 $theme_handler = \Drupal::service('theme_handler'); 1128 $theme_handler = \Drupal::service('theme_handler');