
| Current Path : /var/www/html/rocksensor1/web/core/modules/file/src/Plugin/Field/FieldFormatter/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/rocksensor1/web/core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php |
<?php
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\StringTranslation\ByteSizeMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Formatter that shows the file byte size in a human-readable way.
*/
#[FieldFormatter(
id: 'file_size',
label: new TranslatableMarkup('Bytes (KB, MB, ...)'),
field_types: [
'integer',
],
)]
class FileSize extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = ['#markup' => ByteSizeMarkup::create((int) $item->value)];
}
return $elements;
}
}