3 namespace Drupal\file\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldItemListInterface;
8 * Plugin implementation of the 'file_rss_enclosure' formatter.
11 * id = "file_rss_enclosure",
12 * label = @Translation("RSS enclosure"),
18 class RSSEnclosureFormatter extends FileFormatterBase {
23 public function viewElements(FieldItemListInterface $items, $langcode) {
24 $entity = $items->getEntity();
25 // Add the first file as an enclosure to the RSS item. RSS allows only one
26 // enclosure per item. See: http://wikipedia.org/wiki/RSS_enclosure
27 foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
28 $entity->rss_elements[] = [
31 // In RSS feeds, it is necessary to use absolute URLs. The 'url.site'
32 // cache context is already associated with RSS feed responses, so it
33 // does not need to be specified here.
34 'url' => file_create_url($file->getFileUri()),
35 'length' => $file->getSize(),
36 'type' => $file->getMimeType(),