Rss.php 3.6 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\style\Rss.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\style;
9

10
use Drupal\Component\Utility\SafeMarkup;
11
use Drupal\Core\Form\FormStateInterface;
12

merlinofchaos's avatar
merlinofchaos committed
13 14 15 16
/**
 * Default style plugin to render an RSS feed.
 *
 * @ingroup views_style_plugins
17
 *
18
 * @ViewsStyle(
aspilicious's avatar
aspilicious committed
19
 *   id = "rss",
20 21 22
 *   title = @Translation("RSS Feed"),
 *   help = @Translation("Generates an RSS feed from a view."),
 *   theme = "views_view_rss",
23
 *   display_types = {"feed"}
24 25
 * )
 */
26
class Rss extends StylePluginBase {
27

28 29 30 31 32
  /**
   * Does the style plugin for itself support to add fields to it's output.
   *
   * @var bool
   */
33
  protected $usesRowPlugin = TRUE;
34

35
  public function attachTo(array &$build, $display_id, $path, $title) {
merlinofchaos's avatar
merlinofchaos committed
36
    $url_options = array();
37
    $input = $this->view->getExposedInput();
merlinofchaos's avatar
merlinofchaos committed
38 39 40 41 42
    if ($input) {
      $url_options['query'] = $input;
    }
    $url_options['absolute'] = TRUE;

43
    $url = _url($this->view->getUrl(NULL, $path), $url_options);
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
    // Add the RSS icon to the view.
    $this->view->feed_icon = [
      '#theme' => 'feed_icon',
      '#url' => $url,
      '#title' => $title,
    ];

    // Attach a link to the RSS feed, which is an alternate representation.
    $build['#attached']['html_head_link'][][] = array(
      'rel' => 'alternate',
      'type' => 'application/rss+xml',
      'title' => $title,
      'href' => $url,
    );
merlinofchaos's avatar
merlinofchaos committed
59 60
  }

61 62
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
63

64
    $options['description'] = array('default' => '');
merlinofchaos's avatar
merlinofchaos committed
65 66 67 68

    return $options;
  }

69
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
70
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
71 72 73

    $form['description'] = array(
      '#type' => 'textfield',
74
      '#title' => $this->t('RSS description'),
merlinofchaos's avatar
merlinofchaos committed
75
      '#default_value' => $this->options['description'],
76
      '#description' => $this->t('This will appear in the RSS feed itself.'),
merlinofchaos's avatar
merlinofchaos committed
77 78 79 80 81 82 83 84 85 86
      '#maxlength' => 1024,
    );
  }

  /**
   * Return an array of additional XHTML elements to add to the channel.
   *
   * @return
   *   An array that can be passed to format_xml_elements().
   */
87
  protected function getChannelElements() {
merlinofchaos's avatar
merlinofchaos committed
88 89 90 91 92 93 94 95 96
    return array();
  }

  /**
   * Get RSS feed description.
   *
   * @return string
   *   The string containing the description with the tokens replaced.
   */
97
  public function getDescription() {
merlinofchaos's avatar
merlinofchaos committed
98 99 100
    $description = $this->options['description'];

    // Allow substitutions from the first row.
101
    $description = $this->tokenizeValue($description, 0);
merlinofchaos's avatar
merlinofchaos committed
102 103 104 105

    return $description;
  }

106
  public function render() {
107
    if (empty($this->view->rowPlugin)) {
108
      debug('Drupal\views\Plugin\views\style\Rss: Missing row plugin');
109
      return array();
merlinofchaos's avatar
merlinofchaos committed
110 111 112 113 114 115 116 117 118
    }
    $rows = '';

    // This will be filled in by the row plugin and is used later on in the
    // theming output.
    $this->namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/');

    // Fetch any additional elements for the channel and merge in their
    // namespaces.
119
    $this->channel_elements = $this->getChannelElements();
merlinofchaos's avatar
merlinofchaos committed
120 121 122 123 124 125 126 127
    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;
128
      $rows .= $this->view->rowPlugin->render($row);
merlinofchaos's avatar
merlinofchaos committed
129 130
    }

131 132 133 134
    $build = array(
      '#theme' => $this->themeFunctions(),
      '#view' => $this->view,
      '#options' => $this->options,
135
      '#rows' => SafeMarkup::set($rows),
136
    );
merlinofchaos's avatar
merlinofchaos committed
137
    unset($this->view->row_index);
138
    return $build;
merlinofchaos's avatar
merlinofchaos committed
139
  }
140

merlinofchaos's avatar
merlinofchaos committed
141
}