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