function RSSEnclosureFormatter::viewElements

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

Overrides FormatterInterface::viewElements

File

core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php, line 23

Class

RSSEnclosureFormatter
Plugin implementation of the 'file_rss_enclosure' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    $entity = $items->getEntity();
    // Add the first file as an enclosure to the RSS item. RSS allows only one
    // enclosure per item. See: http://wikipedia.org/wiki/RSS_enclosure
    foreach ($this->getEntitiesToView($items, $langcode) as $file) {
        
        /** @var \Drupal\file\FileInterface $file */
        $entity->rss_elements[] = [
            'key' => 'enclosure',
            'attributes' => [
                // In RSS feeds, it is necessary to use absolute URLs. The 'url.site'
                // cache context is already associated with RSS feed responses, so it
                // does not need to be specified here.
'url' => $file->createFileUrl(FALSE),
                'length' => $file->getSize(),
                'type' => $file->getMimeType(),
            ],
        ];
    }
    return [];
}

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