class GenericFileFormatter

Same name and namespace in other branches
  1. 10 core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter
  2. 11.x core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter
  3. 8.9.x core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter

Plugin implementation of the 'file_default' formatter.

Plugin annotation


@FieldFormatter(
  id = "file_default",
  label = @Translation("Generic file"),
  field_types = {
    "file"
  }
)

Hierarchy

Expanded class hierarchy of GenericFileFormatter

File

core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php, line 18

Namespace

Drupal\file\Plugin\Field\FieldFormatter
View source
class GenericFileFormatter extends DescriptionAwareFileFormatterBase {
  
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
      $item = $file->_referringItem;
      $elements[$delta] = [
        '#theme' => 'file_link',
        '#file' => $file,
        '#description' => $this->getSetting('use_description_as_link_text') ? $item->description : NULL,
        '#cache' => [
          'tags' => $file->getCacheTags(),
        ],
      ];
      // Pass field item attributes to the theme function.
      if (isset($item->_attributes)) {
        $elements[$delta] += [
          '#attributes' => [],
        ];
        $elements[$delta]['#attributes'] += $item->_attributes;
        // Unset field item attributes since they have been included in the
        // formatter output and should not be rendered in the field template.
        unset($item->_attributes);
      }
    }
    return $elements;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.