/core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
PHP | 42 lines | 17 code | 8 blank | 17 comment | 1 complexity | 19ec33a26d3bead18106d5d3e20f8b01 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
- <?php
- namespace Drupal\file\Plugin\Field\FieldFormatter;
- use Drupal\Core\Field\FieldDefinitionInterface;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\Core\Field\FormatterBase;
- /**
- * Formatter that shows the file size in a human readable way.
- *
- * @FieldFormatter(
- * id = "file_size",
- * label = @Translation("File size"),
- * field_types = {
- * "integer"
- * }
- * )
- */
- class FileSize extends FormatterBase {
- /**
- * {@inheritdoc}
- */
- public static function isApplicable(FieldDefinitionInterface $field_definition) {
- return parent::isApplicable($field_definition) && $field_definition->getName() === 'filesize';
- }
- /**
- * {@inheritdoc}
- */
- public function viewElements(FieldItemListInterface $items, $langcode) {
- $elements = [];
- foreach ($items as $delta => $item) {
- $elements[$delta] = ['#markup' => format_size($item->value)];
- }
- return $elements;
- }
- }