function FileExtensionFormatter::viewValue

Same name in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/FileExtensionFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\FileExtensionFormatter::viewValue()
  2. 10 core/modules/file/src/Plugin/Field/FieldFormatter/FileExtensionFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\FileExtensionFormatter::viewValue()
  3. 11.x core/modules/file/src/Plugin/Field/FieldFormatter/FileExtensionFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\FileExtensionFormatter::viewValue()

Overrides BaseFieldFileFormatterBase::viewValue

File

core/modules/file/src/Plugin/Field/FieldFormatter/FileExtensionFormatter.php, line 49

Class

FileExtensionFormatter
Formatter to render a filename as file extension.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

protected function viewValue(FieldItemInterface $item) {
    $filename = $item->value;
    if (!$this->getSetting('extension_detect_tar')) {
        return pathinfo($filename, PATHINFO_EXTENSION);
    }
    else {
        $file_parts = explode('.', basename($filename));
        if (count($file_parts) > 1) {
            $extension = array_pop($file_parts);
            $last_part_in_name = array_pop($file_parts);
            if ($last_part_in_name === 'tar') {
                $extension = 'tar.' . $extension;
            }
            return $extension;
        }
    }
}

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