Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / FileSize.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
7
8 /**
9  * Render a numeric value as a size.
10  *
11  * @ingroup views_field_handlers
12  *
13  * @ViewsField("file_size")
14  */
15 class FileSize extends FieldPluginBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function defineOptions() {
21     $options = parent::defineOptions();
22
23     $options['file_size_display'] = ['default' => 'formatted'];
24
25     return $options;
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
32     parent::buildOptionsForm($form, $form_state);
33     $form['file_size_display'] = [
34       '#title' => $this->t('File size display'),
35       '#type' => 'select',
36       '#options' => [
37         'formatted' => $this->t('Formatted (in KB or MB)'),
38         'bytes' => $this->t('Raw bytes'),
39       ],
40     ];
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function render(ResultRow $values) {
47     $value = $this->getValue($values);
48     if ($value) {
49       switch ($this->options['file_size_display']) {
50         case 'bytes':
51           return $value;
52         case 'formatted':
53         default:
54           return format_size($value);
55       }
56     }
57     else {
58       return '';
59     }
60   }
61
62 }