Version 1
[yaffs-website] / web / core / modules / file / src / Plugin / Field / FieldFormatter / FileSize.php
1 <?php
2
3 namespace Drupal\file\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
8
9 /**
10  * Formatter that shows the file size in a human readable way.
11  *
12  * @FieldFormatter(
13  *   id = "file_size",
14  *   label = @Translation("File size"),
15  *   field_types = {
16  *     "integer"
17  *   }
18  * )
19  */
20 class FileSize extends FormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function isApplicable(FieldDefinitionInterface $field_definition) {
26     return parent::isApplicable($field_definition) && $field_definition->getName() === 'filesize';
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function viewElements(FieldItemListInterface $items, $langcode) {
33     $elements = [];
34
35     foreach ($items as $delta => $item) {
36       $elements[$delta] = ['#markup' => format_size($item->value)];
37     }
38
39     return $elements;
40   }
41
42 }