You are here

class FileSize in Drupal 10

Same name in this branch
  1. 10 core/modules/views/src/Plugin/views/field/FileSize.php \Drupal\views\Plugin\views\field\FileSize
  2. 10 core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php \Drupal\file\Plugin\Field\FieldFormatter\FileSize
Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/FileSize.php \Drupal\views\Plugin\views\field\FileSize
  2. 9 core/modules/views/src/Plugin/views/field/FileSize.php \Drupal\views\Plugin\views\field\FileSize

Render a numeric value as a size.

Plugin annotation

@ViewsField("file_size");

Hierarchy

  • class \Drupal\views\Plugin\views\field\FileSize extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of FileSize

File

core/modules/views/src/Plugin/views/field/FileSize.php, line 15

Namespace

Drupal\views\Plugin\views\field
View source
class FileSize extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['file_size_display'] = [
      'default' => 'formatted',
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['file_size_display'] = [
      '#title' => $this
        ->t('File size display'),
      '#type' => 'select',
      '#options' => [
        'formatted' => $this
          ->t('Formatted (in KB or MB)'),
        'bytes' => $this
          ->t('Raw bytes'),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $value = $this
      ->getValue($values);
    if ($value) {
      switch ($this->options['file_size_display']) {
        case 'bytes':
          return $value;
        case 'formatted':
        default:
          return format_size($value);
      }
    }
    else {
      return '';
    }
  }

}

Members