3 namespace Drupal\file\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
10 * Formatter that shows the file size in a human readable way.
14 * label = @Translation("File size"),
20 class FileSize extends FormatterBase {
25 public static function isApplicable(FieldDefinitionInterface $field_definition) {
26 return parent::isApplicable($field_definition) && $field_definition->getName() === 'filesize';
32 public function viewElements(FieldItemListInterface $items, $langcode) {
35 foreach ($items as $delta => $item) {
36 $elements[$delta] = ['#markup' => format_size($item->value)];