3 namespace Drupal\views\Plugin\views\style;
5 use Drupal\Core\Form\FormStateInterface;
9 * Default style plugin to render an RSS feed.
11 * @ingroup views_style_plugins
15 * title = @Translation("RSS Feed"),
16 * help = @Translation("Generates an RSS feed from a view."),
17 * theme = "views_view_rss",
18 * display_types = {"feed"}
21 class Rss extends StylePluginBase {
26 protected $usesRowPlugin = TRUE;
28 public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
30 $input = $this->view->getExposedInput();
32 $url_options['query'] = $input;
34 $url_options['absolute'] = TRUE;
36 $url = $feed_url->setOptions($url_options)->toString();
38 // Add the RSS icon to the view.
39 $this->view->feedIcons[] = [
40 '#theme' => 'feed_icon',
45 // Attach a link to the RSS feed, which is an alternate representation.
46 $build['#attached']['html_head_link'][][] = [
48 'type' => 'application/rss+xml',
54 protected function defineOptions() {
55 $options = parent::defineOptions();
57 $options['description'] = ['default' => ''];
62 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
63 parent::buildOptionsForm($form, $form_state);
65 $form['description'] = [
66 '#type' => 'textfield',
67 '#title' => $this->t('RSS description'),
68 '#default_value' => $this->options['description'],
69 '#description' => $this->t('This will appear in the RSS feed itself.'),
75 * Return an array of additional XHTML elements to add to the channel.
80 protected function getChannelElements() {
85 * Get RSS feed description.
88 * The string containing the description with the tokens replaced.
90 public function getDescription() {
91 $description = $this->options['description'];
93 // Allow substitutions from the first row.
94 $description = $this->tokenizeValue($description, 0);
99 public function render() {
100 if (empty($this->view->rowPlugin)) {
101 debug('Drupal\views\Plugin\views\style\Rss: Missing row plugin');
106 // This will be filled in by the row plugin and is used later on in the
108 $this->namespaces = ['xmlns:dc' => 'http://purl.org/dc/elements/1.1/'];
110 // Fetch any additional elements for the channel and merge in their
112 $this->channel_elements = $this->getChannelElements();
113 foreach ($this->channel_elements as $element) {
114 if (isset($element['namespace'])) {
115 $this->namespaces = array_merge($this->namespaces, $element['namespace']);
119 foreach ($this->view->result as $row_index => $row) {
120 $this->view->row_index = $row_index;
121 $rows[] = $this->view->rowPlugin->render($row);
125 '#theme' => $this->themeFunctions(),
126 '#view' => $this->view,
127 '#options' => $this->options,
130 unset($this->view->row_index);