Skip to content

Commit c5bb1a0

Browse files
committed
Add enclosure field.
1 parent 7cd3359 commit c5bb1a0

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ Drupal 8 module for providing <itunes:*> tags via Views RSS.
33
# Set up
44

55
1. Create a new view with a 'feed' display.
6-
1. Add the entity fields that will be rendered as iTunes elements. E.g., add the file field that should be rendered as the <enclosure>.
6+
1. Add the entity fields that will be rendered as iTunes elements. E.g., add the file field that should be rendered as the `<enclosure>`.
77
1. Select "iTunes RSS Feed" as the display __format__.
88
1. Set <channel> settings under settings for "iTunes RSS Feed".
99
1. Select "iTunes Fields" under format's __show__ setting.
1010
1. Set the <item> settings under settings for "iTunes Fields".
11+
12+
# Customize
13+
14+
You may customize the output using Drupal's core theming system. The following twig templates may be overridden:
15+
16+
* views-view-itunes-rss.html.twig
17+
* views-view-row-rss.html.twig
18+
19+
The following preprocessor may be overridden: `template_preprocess_views_view_itunes_rss()`.

itunes_rss.module

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,11 @@ function itunes_rss_theme() {
3434
* - rows: The raw row data.
3535
*/
3636
function template_preprocess_views_view_itunes_rss(&$variables) {
37-
// \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
3837
template_preprocess_views_view_rss($variables);
3938

4039
$view = $variables['view'];
4140
$items = $variables['rows'];
4241
$style = $view->style_plugin;
43-
44-
// may need to create getCategory() on viewsRss.
42+
$variables['image_url'] = $style->getImage();
4543
$variables['itunes_category'] = $style->options['category'];
46-
4744
}

src/Plugin/views/row/ItunesRssFields.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Drupal\itunes_rss\Plugin\views\row;
44

55
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\file\Entity\File;
7+
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
68
use Drupal\views\Plugin\views\row\RssFields;
79

810
/**
@@ -23,7 +25,7 @@ class ItunesRssFields extends RssFields {
2325
*/
2426
protected function defineOptions() {
2527
$options = parent::defineOptions();
26-
28+
$options['enclosure_field'] = ['default' => ''];
2729
foreach ($this->getItunesItemFields() as $field) {
2830
$options['itunes']['contains'][$this->getItunesFieldMachineName($field)] = ['default' => ''];
2931
}
@@ -92,6 +94,14 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
9294
$view_fields_labels = $this->displayHandler->getFieldLabels();
9395
$view_fields_labels = array_merge($initial_labels, $view_fields_labels);
9496

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+
95105
$form['itunes'] = [
96106
'#type' => 'details',
97107
'#title' => $this->t('iTunes fields'),
@@ -123,6 +133,28 @@ public function render($row) {
123133
$row_index = 0;
124134
}
125135
$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+
126158
$fields = $this->getItunesItemFields();
127159

128160
// Render boolean fields as yes/no.

0 commit comments

Comments
 (0)