function FileMediaFormatterBase::viewElements

Same name in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::viewElements()
  2. 8.9.x core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::viewElements()
  3. 10 core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::viewElements()

Overrides FormatterInterface::viewElements

File

core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php, line 114

Class

FileMediaFormatterBase
Base class for media file formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    $source_files = $this->getSourceFiles($items, $langcode);
    if (empty($source_files)) {
        return $elements;
    }
    $attributes = $this->prepareAttributes();
    foreach ($source_files as $delta => $files) {
        $elements[$delta] = [
            '#theme' => $this->getPluginId(),
            '#attributes' => $attributes,
            '#files' => $files,
            '#cache' => [
                'tags' => [],
            ],
        ];
        $cache_tags = [];
        foreach ($files as $file) {
            $cache_tags = Cache::mergeTags($cache_tags, $file['file']->getCacheTags());
        }
        $elements[$delta]['#cache']['tags'] = $cache_tags;
    }
    return $elements;
}

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