Version 1
[yaffs-website] / web / core / modules / file / src / Plugin / Field / FieldFormatter / RSSEnclosureFormatter.php
diff --git a/web/core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php b/web/core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php
new file mode 100644 (file)
index 0000000..a433d99
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace Drupal\file\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Field\FieldItemListInterface;
+
+/**
+ * Plugin implementation of the 'file_rss_enclosure' formatter.
+ *
+ * @FieldFormatter(
+ *   id = "file_rss_enclosure",
+ *   label = @Translation("RSS enclosure"),
+ *   field_types = {
+ *     "file"
+ *   }
+ * )
+ */
+class RSSEnclosureFormatter extends FileFormatterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  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 $delta => $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_create_url($file->getFileUri()),
+          'length' => $file->getSize(),
+          'type' => $file->getMimeType(),
+        ],
+      ];
+    }
+    return [];
+  }
+
+}