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 {
24 * Does the style plugin for itself support to add fields to it's output.
28 protected $usesRowPlugin = TRUE;
30 public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
32 $input = $this->view->getExposedInput();
34 $url_options['query'] = $input;
36 $url_options['absolute'] = TRUE;
38 $url = $feed_url->setOptions($url_options)->toString();
40 // Add the RSS icon to the view.
41 $this->view->feedIcons[] = [
42 '#theme' => 'feed_icon',
47 // Attach a link to the RSS feed, which is an alternate representation.
48 $build['#attached']['html_head_link'][][] = [
50 'type' => 'application/rss+xml',
56 protected function defineOptions() {
57 $options = parent::defineOptions();
59 $options['description'] = ['default' => ''];
64 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
65 parent::buildOptionsForm($form, $form_state);
67 $form['description'] = [
68 '#type' => 'textfield',
69 '#title' => $this->t('RSS description'),
70 '#default_value' => $this->options['description'],
71 '#description' => $this->t('This will appear in the RSS feed itself.'),
77 * Return an array of additional XHTML elements to add to the channel.
82 protected function getChannelElements() {
87 * Get RSS feed description.
90 * The string containing the description with the tokens replaced.
92 public function getDescription() {
93 $description = $this->options['description'];
95 // Allow substitutions from the first row.
96 $description = $this->tokenizeValue($description, 0);
101 public function render() {
102 if (empty($this->view->rowPlugin)) {
103 debug('Drupal\views\Plugin\views\style\Rss: Missing row plugin');
108 // This will be filled in by the row plugin and is used later on in the
110 $this->namespaces = ['xmlns:dc' => 'http://purl.org/dc/elements/1.1/'];
112 // Fetch any additional elements for the channel and merge in their
114 $this->channel_elements = $this->getChannelElements();
115 foreach ($this->channel_elements as $element) {
116 if (isset($element['namespace'])) {
117 $this->namespaces = array_merge($this->namespaces, $element['namespace']);
121 foreach ($this->view->result as $row_index => $row) {
122 $this->view->row_index = $row_index;
123 $rows[] = $this->view->rowPlugin->render($row);
127 '#theme' => $this->themeFunctions(),
128 '#view' => $this->view,
129 '#options' => $this->options,
132 unset($this->view->row_index);