a433d99b16e7a116102a41f9460ef486084fd960
[yaffs-website] / web / core / modules / file / src / Plugin / Field / FieldFormatter / RSSEnclosureFormatter.php
1 <?php
2
3 namespace Drupal\file\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6
7 /**
8  * Plugin implementation of the 'file_rss_enclosure' formatter.
9  *
10  * @FieldFormatter(
11  *   id = "file_rss_enclosure",
12  *   label = @Translation("RSS enclosure"),
13  *   field_types = {
14  *     "file"
15  *   }
16  * )
17  */
18 class RSSEnclosureFormatter extends FileFormatterBase {
19
20   /**
21    * {@inheritdoc}
22    */
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[] = [
29         'key' => 'enclosure',
30         'attributes' => [
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(),
37         ],
38       ];
39     }
40     return [];
41   }
42
43 }