diff --git a/core/includes/common.inc b/core/includes/common.inc index 49cf6e104a..46a7b8fc82 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -252,19 +252,20 @@ function check_url($uri) { */ function format_size($size, $langcode = NULL) { if ($size < Bytes::KILOBYTE) { - return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]); + if ($size == -1) { + return \Drupal::translation()->t('-1 byte', [], ['langcode' => $langcode]); + } + else { + return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]); + } } else { // Convert bytes to kilobytes. - $size = $size / Bytes::KILOBYTE; - $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - foreach ($units as $unit) { - if (round($size, 2) >= Bytes::KILOBYTE) { - $size = $size / Bytes::KILOBYTE; - } - else { + foreach (['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) { + if (round($size, 2) < Bytes::KILOBYTE) { break; } + $size = $size / Bytes::KILOBYTE; } $args = ['@size' => round($size, 2)]; $options = ['langcode' => $langcode]; diff --git a/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php b/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php index d86ed10bd6..e821cb01fa 100644 --- a/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php +++ b/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php @@ -21,7 +21,7 @@ protected function setUp() { $this->exactTestCases = [ '0 bytes' => 0, '1 byte' => 1, - '-1 bytes' => -1, + '-1 byte' => -1, '1 KB' => $kb, '1 MB' => $kb * $kb, '1 GB' => $kb * $kb * $kb,