From 282d7ebc6ab81d8c1cfb0077b372c646cf890259 Mon Sep 17 00:00:00 2001
From: Matthew Grasmick <matt.grasmick@gmail.com>
Date: Thu, 16 Nov 2017 20:13:25 -0500
Subject: [PATCH] Issue #2511878: Support enclosure field in Views RssFields
 row plugin.

---
 .../views/src/Plugin/views/row/RssFields.php       | 31 ++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/core/modules/views/src/Plugin/views/row/RssFields.php b/core/modules/views/src/Plugin/views/row/RssFields.php
index 1b56aae5f1..b25430ff5a 100644
--- a/core/modules/views/src/Plugin/views/row/RssFields.php
+++ b/core/modules/views/src/Plugin/views/row/RssFields.php
@@ -4,6 +4,8 @@
 
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
+use Drupal\file\Entity\File;
+use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
 
 /**
  * Renders an RSS item based on fields.
@@ -32,6 +34,7 @@ protected function defineOptions() {
     $options['description_field'] = ['default' => ''];
     $options['creator_field'] = ['default' => ''];
     $options['date_field'] = ['default' => ''];
+    $options['enclosure_field'] = ['default' => ''];
     $options['guid_field_options']['contains']['guid_field'] = ['default' => ''];
     $options['guid_field_options']['contains']['guid_field_is_permalink'] = ['default' => TRUE];
     return $options;
@@ -84,6 +87,13 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
       '#default_value' => $this->options['date_field'],
       '#required' => TRUE,
     ];
+    $form['enclosure_field'] = [
+      '#type' => 'select',
+      '#title' => $this->t('Enclosure field'),
+      '#description' => $this->t('Describes a media object that is attached to the item. This must be a file field.'),
+      '#options' => $view_fields_labels,
+      '#default_value' => $this->options['enclosure_field'],
+    ];
     $form['guid_field_options'] = [
       '#type' => 'details',
       '#title' => $this->t('GUID settings'),
@@ -154,6 +164,27 @@ public function render($row) {
         'namespace' => ['xmlns:dc' => 'http://purl.org/dc/elements/1.1/'],
       ],
     ];
+    if ($this->options['enclosure_field']) {
+      $field_name = $this->options['enclosure_field'];
+      $entity = $this->view->result[$row_index]->_entity;
+      $enclosure = $entity->$field_name;
+      if ($enclosure instanceof FileFieldItemList) {
+        $value = $enclosure->getValue();
+        $file = File::load($value[0]['target_id']);
+        $item->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(),
+          ]
+        ];
+      }
+    }
+
     $guid_is_permalink_string = 'false';
     $item_guid = $this->getField($row_index, $this->options['guid_field_options']['guid_field']);
     if ($this->options['guid_field_options']['guid_field_is_permalink']) {
-- 
2.12.2

