3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
9 * Render a numeric value as a size.
11 * @ingroup views_field_handlers
13 * @ViewsField("file_size")
15 class FileSize extends FieldPluginBase {
20 protected function defineOptions() {
21 $options = parent::defineOptions();
23 $options['file_size_display'] = ['default' => 'formatted'];
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'),
37 'formatted' => $this->t('Formatted (in KB or MB)'),
38 'bytes' => $this->t('Raw bytes'),
46 public function render(ResultRow $values) {
47 $value = $this->getValue($values);
49 switch ($this->options['file_size_display']) {
54 return format_size($value);