View source
<?php
namespace Drupal\views\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class Rss extends StylePluginBase {
protected $usesRowPlugin = TRUE;
public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
$url_options = [];
$input = $this->view
->getExposedInput();
if ($input) {
$url_options['query'] = $input;
}
$url_options['absolute'] = TRUE;
$url = $feed_url
->setOptions($url_options)
->toString();
$this->view->feedIcons[] = [
'#theme' => 'feed_icon',
'#url' => $url,
'#title' => $title,
];
$build['#attached']['html_head_link'][][] = [
'rel' => 'alternate',
'type' => 'application/rss+xml',
'title' => $title,
'href' => $url,
];
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['description'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('RSS description'),
'#default_value' => $this->options['description'],
'#description' => $this
->t('This will appear in the RSS feed itself.'),
'#maxlength' => 1024,
];
}
protected function getChannelElements() {
return [];
}
public function getDescription() {
$description = $this->options['description'];
$description = $this
->tokenizeValue($description, 0);
return $description;
}
public function render() {
if (empty($this->view->rowPlugin)) {
trigger_error('Drupal\\views\\Plugin\\views\\style\\Rss: Missing row plugin', E_WARNING);
return [];
}
$rows = [];
$this->namespaces = [
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
];
$this->channel_elements = $this
->getChannelElements();
foreach ($this->channel_elements as $element) {
if (isset($element['namespace'])) {
$this->namespaces = array_merge($this->namespaces, $element['namespace']);
}
}
foreach ($this->view->result as $row_index => $row) {
$this->view->row_index = $row_index;
$rows[] = $this->view->rowPlugin
->render($row);
}
$build = [
'#theme' => $this
->themeFunctions(),
'#view' => $this->view,
'#options' => $this->options,
'#rows' => $rows,
];
unset($this->view->row_index);
return $build;
}
}