You are here

protected function FileExtensionFormatter::viewValue in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Generate the output appropriate for one field item.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: One field item.

Return value

mixed The textual output generated.

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