function ByteSizeMarkup::create

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php \Drupal\Core\StringTranslation\ByteSizeMarkup::create()

Gets the TranslatableMarkup object for the provided size.

Return value

\Drupal\Core\StringTranslation\TranslatableMarkup The translatable markup.

Throws

\LogicException Thrown when an invalid unit size is used.

20 calls to ByteSizeMarkup::create()
ByteSizeMarkupTest::testCommonFormatSize in core/tests/Drupal/Tests/Core/StringTranslation/ByteSizeMarkupTest.php
@covers ::create[[api-linebreak]] @dataProvider providerTestCommonFormatSize
ByteSizeMarkupTest::testTranslatableMarkupObject in core/tests/Drupal/Tests/Core/StringTranslation/ByteSizeMarkupTest.php
@covers ::create[[api-linebreak]]
editor_image_upload_settings_form in core/modules/editor/editor.admin.inc
Subform constructor to configure the text editor's image upload settings.
FileFieldValidateTest::testFileMaxSize in core/modules/file/tests/src/Functional/FileFieldValidateTest.php
Tests the max file size validator.
FileItem::fieldSettingsForm in core/modules/file/src/Plugin/Field/FieldType/FileItem.php

... See full list

File

core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php, line 28

Class

ByteSizeMarkup
A class to generate translatable markup for the given byte count.

Namespace

Drupal\Core\StringTranslation

Code

public static function create(float|int $size, ?string $langcode = NULL, ?TranslationInterface $stringTranslation = NULL) : TranslatableMarkup {
  $options = [
    'langcode' => $langcode,
  ];
  $absolute_size = abs($size);
  if ($absolute_size < Bytes::KILOBYTE) {
    return new PluralTranslatableMarkup($size, '1 byte', '@count bytes', [], $options, $stringTranslation);
  }
  // Create a multiplier to preserve the sign of $size.
  $sign = $absolute_size / $size;
  foreach ([
    'KB',
    'MB',
    'GB',
    'TB',
    'PB',
    'EB',
    'ZB',
    'YB',
  ] as $unit) {
    $absolute_size /= Bytes::KILOBYTE;
    $rounded_size = round($absolute_size, 2);
    if ($rounded_size < Bytes::KILOBYTE) {
      break;

    }
  }
  $args = [
    '@size' => $rounded_size * $sign,
  ];
  // At this point $markup must be set.
  return match ($unit) {  'KB' => new TranslatableMarkup('@size KB', $args, $options, $stringTranslation),
    'MB' => new TranslatableMarkup('@size MB', $args, $options, $stringTranslation),
    'GB' => new TranslatableMarkup('@size GB', $args, $options, $stringTranslation),
    'TB' => new TranslatableMarkup('@size TB', $args, $options, $stringTranslation),
    'PB' => new TranslatableMarkup('@size PB', $args, $options, $stringTranslation),
    'EB' => new TranslatableMarkup('@size EB', $args, $options, $stringTranslation),
    'ZB' => new TranslatableMarkup('@size ZB', $args, $options, $stringTranslation),
    'YB' => new TranslatableMarkup('@size YB', $args, $options, $stringTranslation),
    default => throw new \LogicException("Unexpected unit value"),
  
  };
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.