PageRenderTime 35ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/field/FileSize.php

http://github.com/drupal/drupal
PHP | 62 lines | 37 code | 9 blank | 16 comment | 3 complexity | e352392e932871ff514718a76f36f165 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\views\Plugin\views\field;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\views\ResultRow;
  5. /**
  6. * Render a numeric value as a size.
  7. *
  8. * @ingroup views_field_handlers
  9. *
  10. * @ViewsField("file_size")
  11. */
  12. class FileSize extends FieldPluginBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected function defineOptions() {
  17. $options = parent::defineOptions();
  18. $options['file_size_display'] = ['default' => 'formatted'];
  19. return $options;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  25. parent::buildOptionsForm($form, $form_state);
  26. $form['file_size_display'] = [
  27. '#title' => $this->t('File size display'),
  28. '#type' => 'select',
  29. '#options' => [
  30. 'formatted' => $this->t('Formatted (in KB or MB)'),
  31. 'bytes' => $this->t('Raw bytes'),
  32. ],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function render(ResultRow $values) {
  39. $value = $this->getValue($values);
  40. if ($value) {
  41. switch ($this->options['file_size_display']) {
  42. case 'bytes':
  43. return $value;
  44. case 'formatted':
  45. default:
  46. return format_size($value);
  47. }
  48. }
  49. else {
  50. return '';
  51. }
  52. }
  53. }