b7efa578d75b1c749be405761c47c82045ea05d5
[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    * Does the style plugin for itself support to add fields to it's output.
25    *
26    * @var bool
27    */
28   protected $usesRowPlugin = TRUE;
29
30   public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
31     $url_options = [];
32     $input = $this->view->getExposedInput();
33     if ($input) {
34       $url_options['query'] = $input;
35     }
36     $url_options['absolute'] = TRUE;
37
38     $url = $feed_url->setOptions($url_options)->toString();
39
40     // Add the RSS icon to the view.
41     $this->view->feedIcons[] = [
42       '#theme' => 'feed_icon',
43       '#url' => $url,
44       '#title' => $title,
45     ];
46
47     // Attach a link to the RSS feed, which is an alternate representation.
48     $build['#attached']['html_head_link'][][] = [
49       'rel' => 'alternate',
50       'type' => 'application/rss+xml',
51       'title' => $title,
52       'href' => $url,
53     ];
54   }
55
56   protected function defineOptions() {
57     $options = parent::defineOptions();
58
59     $options['description'] = ['default' => ''];
60
61     return $options;
62   }
63
64   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
65     parent::buildOptionsForm($form, $form_state);
66
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.'),
72       '#maxlength' => 1024,
73     ];
74   }
75
76   /**
77    * Return an array of additional XHTML elements to add to the channel.
78    *
79    * @return
80    *   A render array.
81    */
82   protected function getChannelElements() {
83     return [];
84   }
85
86   /**
87    * Get RSS feed description.
88    *
89    * @return string
90    *   The string containing the description with the tokens replaced.
91    */
92   public function getDescription() {
93     $description = $this->options['description'];
94
95     // Allow substitutions from the first row.
96     $description = $this->tokenizeValue($description, 0);
97
98     return $description;
99   }
100
101   public function render() {
102     if (empty($this->view->rowPlugin)) {
103       debug('Drupal\views\Plugin\views\style\Rss: Missing row plugin');
104       return [];
105     }
106     $rows = [];
107
108     // This will be filled in by the row plugin and is used later on in the
109     // theming output.
110     $this->namespaces = ['xmlns:dc' => 'http://purl.org/dc/elements/1.1/'];
111
112     // Fetch any additional elements for the channel and merge in their
113     // namespaces.
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']);
118       }
119     }
120
121     foreach ($this->view->result as $row_index => $row) {
122       $this->view->row_index = $row_index;
123       $rows[] = $this->view->rowPlugin->render($row);
124     }
125
126     $build = [
127       '#theme' => $this->themeFunctions(),
128       '#view' => $this->view,
129       '#options' => $this->options,
130       '#rows' => $rows,
131     ];
132     unset($this->view->row_index);
133     return $build;
134   }
135
136 }