PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/drupal/drupal
PHP | 42 lines | 17 code | 8 blank | 17 comment | 1 complexity | 19ec33a26d3bead18106d5d3e20f8b01 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\file\Plugin\Field\FieldFormatter;
  3. use Drupal\Core\Field\FieldDefinitionInterface;
  4. use Drupal\Core\Field\FieldItemListInterface;
  5. use Drupal\Core\Field\FormatterBase;
  6. /**
  7. * Formatter that shows the file size in a human readable way.
  8. *
  9. * @FieldFormatter(
  10. * id = "file_size",
  11. * label = @Translation("File size"),
  12. * field_types = {
  13. * "integer"
  14. * }
  15. * )
  16. */
  17. class FileSize extends FormatterBase {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function isApplicable(FieldDefinitionInterface $field_definition) {
  22. return parent::isApplicable($field_definition) && $field_definition->getName() === 'filesize';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function viewElements(FieldItemListInterface $items, $langcode) {
  28. $elements = [];
  29. foreach ($items as $delta => $item) {
  30. $elements[$delta] = ['#markup' => format_size($item->value)];
  31. }
  32. return $elements;
  33. }
  34. }