class FileSize

Same name in this branch
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php \Drupal\file\Plugin\Field\FieldFormatter\FileSize
Same name and namespace in other branches
  1. 11.x core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php \Drupal\file\Plugin\Field\FieldFormatter\FileSize
  2. 11.x 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

Expanded class hierarchy of FileSize

Related topics

6 string references to 'FileSize'
FileAccessControlHandler::checkFieldAccess in core/modules/file/src/FileAccessControlHandler.php
Default field access as determined by this access control handler.
FileEntityFormatterTest::testFormatterFileSize in core/modules/file/tests/src/Kernel/Formatter/FileEntityFormatterTest.php
Tests the file_size field formatter.
FileSize::isApplicable in core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
Returns if the formatter can be used for the provided field.
FileStorage::spaceUsed in core/modules/file/src/FileStorage.php
Determines total disk space used by a single user or the whole filesystem.
FileViewsFieldAccessTest::testFileFields in core/modules/file/tests/src/Kernel/Views/FileViewsFieldAccessTest.php
Check access for file fields.

... See full list

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 '';
    }
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.