You are here

protected function FileExtensionFormatter::viewValue in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 54
Contains \Drupal\file\Plugin\Field\FieldFormatter\FileExtensionFormatter.

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