3
3
namespace Drupal \itunes_rss \Plugin \views \row ;
4
4
5
5
use Drupal \Core \Form \FormStateInterface ;
6
+ use Drupal \file \Entity \File ;
7
+ use Drupal \file \Plugin \Field \FieldType \FileFieldItemList ;
6
8
use Drupal \views \Plugin \views \row \RssFields ;
7
9
8
10
/**
@@ -23,7 +25,7 @@ class ItunesRssFields extends RssFields {
23
25
*/
24
26
protected function defineOptions () {
25
27
$ options = parent ::defineOptions ();
26
-
28
+ $ options [ ' enclosure_field ' ] = [ ' default ' => '' ];
27
29
foreach ($ this ->getItunesItemFields () as $ field ) {
28
30
$ options ['itunes ' ]['contains ' ][$ this ->getItunesFieldMachineName ($ field )] = ['default ' => '' ];
29
31
}
@@ -92,6 +94,14 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
92
94
$ view_fields_labels = $ this ->displayHandler ->getFieldLabels ();
93
95
$ view_fields_labels = array_merge ($ initial_labels , $ view_fields_labels );
94
96
97
+ $ form ['enclosure_field ' ] = [
98
+ '#type ' => 'select ' ,
99
+ '#title ' => $ this ->t ('Enclosure field ' ),
100
+ '#description ' => $ this ->t ('Describes a media object that is attached to the item. This must be a file field. ' ),
101
+ '#options ' => $ view_fields_labels ,
102
+ '#default_value ' => $ this ->options ['enclosure_field ' ],
103
+ ];
104
+
95
105
$ form ['itunes ' ] = [
96
106
'#type ' => 'details ' ,
97
107
'#title ' => $ this ->t ('iTunes fields ' ),
@@ -123,6 +133,28 @@ public function render($row) {
123
133
$ row_index = 0 ;
124
134
}
125
135
$ item = $ build ['#row ' ];
136
+
137
+ if ($ this ->options ['enclosure_field ' ]) {
138
+ $ field_name = $ this ->options ['enclosure_field ' ];
139
+ $ entity = $ this ->view ->result [$ row_index ]->_entity ;
140
+ $ enclosure = $ entity ->$ field_name ;
141
+ if ($ enclosure instanceof FileFieldItemList) {
142
+ $ value = $ enclosure ->getValue ();
143
+ $ file = File::load ($ value [0 ]['target_id ' ]);
144
+ $ item ->elements [] = [
145
+ 'key ' => 'enclosure ' ,
146
+ 'attributes ' => [
147
+ // In RSS feeds, it is necessary to use absolute URLs. The 'url.site'
148
+ // cache context is already associated with RSS feed responses, so it
149
+ // does not need to be specified here.
150
+ 'url ' => file_create_url ($ file ->getFileUri ()),
151
+ 'length ' => $ file ->getSize (),
152
+ 'type ' => $ file ->getMimeType (),
153
+ ]
154
+ ];
155
+ }
156
+ }
157
+
126
158
$ fields = $ this ->getItunesItemFields ();
127
159
128
160
// Render boolean fields as yes/no.
0 commit comments