FileSize.php

Same filename in this branch
  1. 10 core/modules/views/src/Plugin/views/field/FileSize.php
Same filename in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
  2. 9 core/modules/views/src/Plugin/views/field/FileSize.php
  3. 8.9.x core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
  4. 8.9.x core/modules/views/src/Plugin/views/field/FileSize.php
  5. 11.x core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
  6. 11.x core/modules/views/src/Plugin/views/field/FileSize.php

Namespace

Drupal\file\Plugin\Field\FieldFormatter

File

core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php

View source
<?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.
 */
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;
    }

}

Classes

Title Deprecated Summary
FileSize Formatter that shows the file byte size in a human-readable way.

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