FileExtensionFormatter.php in Drupal 10
File
core/modules/file/src/Plugin/Field/FieldFormatter/FileExtensionFormatter.php
View source
<?php
namespace Drupal\file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormStateInterface;
class FileExtensionFormatter extends BaseFieldFileFormatterBase {
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['extension_detect_tar'] = FALSE;
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['extension_detect_tar'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include tar in extension'),
'#description' => $this
->t("If the part of the filename just before the extension is '.tar', include this in the extension output."),
'#default_value' => $this
->getSetting('extension_detect_tar'),
];
return $form;
}
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;
}
}
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition
->getName() === 'filename';
}
}