Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 31 |
| FileExtensionFormatter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 31 |
| defaultSettings | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
| settingsForm | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 9 |
|||
| viewValue | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 16 |
|||
| isApplicable | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 2 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\file\Plugin\Field\FieldFormatter\FileExtensionFormatter. | |
| */ | |
| namespace Drupal\file\Plugin\Field\FieldFormatter; | |
| use Drupal\Core\Field\FieldDefinitionInterface; | |
| use Drupal\Core\Field\FieldItemInterface; | |
| use Drupal\Core\Form\FormStateInterface; | |
| /** | |
| * Formatter to render a filename as file extension. | |
| * | |
| * @FieldFormatter( | |
| * id = "file_extension", | |
| * label = @Translation("File extension"), | |
| * field_types = { | |
| * "string" | |
| * } | |
| * ) | |
| */ | |
| class FileExtensionFormatter extends BaseFieldFileFormatterBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function defaultSettings() { | |
| $settings = parent::defaultSettings(); | |
| $settings['extension_detect_tar'] = FALSE; | |
| return $settings; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function settingsForm(array $form, FormStateInterface $form_state) { | |
| $form = parent::settingsForm($form, $form_state); | |
| $form['extension_detect_tar'] = array( | |
| '#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; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| 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; | |
| } | |
| } | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function isApplicable(FieldDefinitionInterface $field_definition) { | |
| // Just show this file extension formatter on the filename field. | |
| return parent::isApplicable($field_definition) && $field_definition->getName() === 'filename'; | |
| } | |
| } |