Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / style / Rss.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\style;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Url;
7
8 /**
9  * Default style plugin to render an RSS feed.
10  *
11  * @ingroup views_style_plugins
12  *
13  * @ViewsStyle(
14  *   id = "rss",
15  *   title = @Translation("RSS Feed"),
16  *   help = @Translation("Generates an RSS feed from a view."),
17  *   theme = "views_view_rss",
18  *   display_types = {"feed"}
19  * )
20  */
21 class Rss extends StylePluginBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   protected $usesRowPlugin = TRUE;
27
28   public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
29     $url_options = [];
30     $input = $this->view->getExposedInput();
31     if ($input) {
32       $url_options['query'] = $input;
33     }
34     $url_options['absolute'] = TRUE;
35
36     $url = $feed_url->setOptions($url_options)->toString();
37
38     // Add the RSS icon to the view.
39     $this->view->feedIcons[] = [
40       '#theme' => 'feed_icon',
41       '#url' => $url,
42       '#title' => $title,
43     ];
44
45     // Attach a link to the RSS feed, which is an alternate representation.
46     $build['#attached']['html_head_link'][][] = [
47       'rel' => 'alternate',
48       'type' => 'application/rss+xml',
49       'title' => $title,
50       'href' => $url,
51     ];
52   }
53
54   protected function defineOptions() {
55     $options = parent::defineOptions();
56
57     $options['description'] = ['default' => ''];
58
59     return $options;
60   }
61
62   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
63     parent::buildOptionsForm($form, $form_state);
64
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.'),
70       '#maxlength' => 1024,
71     ];
72   }
73
74   /**
75    * Return an array of additional XHTML elements to add to the channel.
76    *
77    * @return
78    *   A render array.
79    */
80   protected function getChannelElements() {
81     return [];
82   }
83
84   /**
85    * Get RSS feed description.
86    *
87    * @return string
88    *   The string containing the description with the tokens replaced.
89    */
90   public function getDescription() {
91     $description = $this->options['description'];
92
93     // Allow substitutions from the first row.
94     $description = $this->tokenizeValue($description, 0);
95
96     return $description;
97   }
98
99   public function render() {
100     if (empty($this->view->rowPlugin)) {
101       debug('Drupal\views\Plugin\views\style\Rss: Missing row plugin');
102       return [];
103     }
104     $rows = [];
105
106     // This will be filled in by the row plugin and is used later on in the
107     // theming output.
108     $this->namespaces = ['xmlns:dc' => 'http://purl.org/dc/elements/1.1/'];
109
110     // Fetch any additional elements for the channel and merge in their
111     // namespaces.
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']);
116       }
117     }
118
119     foreach ($this->view->result as $row_index => $row) {
120       $this->view->row_index = $row_index;
121       $rows[] = $this->view->rowPlugin->render($row);
122     }
123
124     $build = [
125       '#theme' => $this->themeFunctions(),
126       '#view' => $this->view,
127       '#options' => $this->options,
128       '#rows' => $rows,
129     ];
130     unset($this->view->row_index);
131     return $build;
132   }
133
134 }