Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

FileSize.php

Same filename in this branch
  1. 11.1.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.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal