3 namespace Drupal\file\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Formatter to render the file MIME type, with an optional icon.
13 * id = "file_filemime",
14 * label = @Translation("File MIME"),
20 class FilemimeFormatter extends BaseFieldFileFormatterBase {
25 public static function isApplicable(FieldDefinitionInterface $field_definition) {
26 return parent::isApplicable($field_definition) && $field_definition->getName() === 'filemime';
32 public static function defaultSettings() {
33 $settings = parent::defaultSettings();
35 $settings['filemime_image'] = FALSE;
43 public function settingsForm(array $form, FormStateInterface $form_state) {
44 $form = parent::settingsForm($form, $form_state);
46 $form['filemime_image'] = [
47 '#title' => $this->t('Display an icon'),
48 '#description' => $this->t('The icon is representing the file type, instead of the MIME text (such as "image/jpeg")'),
49 '#type' => 'checkbox',
50 '#default_value' => $this->getSetting('filemime_image'),
59 protected function viewValue(FieldItemInterface $item) {
60 $value = $item->value;
61 if ($this->getSetting('filemime_image') && $value) {
63 '#theme' => 'image__file_icon',
64 '#file' => $item->getEntity(),