Rss.php 3.93 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) {
36
    $display = $this->view->displayHandlers->get($display_id);
merlinofchaos's avatar
merlinofchaos committed
37
    $url_options = array();
38
    $input = $this->view->getExposedInput();
merlinofchaos's avatar
merlinofchaos committed
39 40 41 42 43
    if ($input) {
      $url_options['query'] = $input;
    }
    $url_options['absolute'] = TRUE;

44
    $url = url($this->view->getUrl(NULL, $path), $url_options);
45
    if ($display->hasPath()) {
merlinofchaos's avatar
merlinofchaos committed
46
      if (empty($this->preview)) {
47
        // Add a call for drupal_add_feed to the view attached data.
48
        $build['#attached']['drupal_add_feed'][] = array($url, $title);
merlinofchaos's avatar
merlinofchaos committed
49 50 51
      }
    }
    else {
52 53 54 55 56
      $feed_icon = array(
        '#theme' => 'feed_icon',
        '#url' => $url,
        '#title' => $title,
      );
57 58 59 60
      $this->view->feed_icon = $feed_icon;

      // Add a call for drupal_add_html_head_link to the view attached data.
      $build['#attached']['drupal_add_html_head_link'][][] = array(
merlinofchaos's avatar
merlinofchaos committed
61 62 63
        'rel' => 'alternate',
        'type' => 'application/rss+xml',
        'title' => $title,
64 65
        'href' => $url,
      );
merlinofchaos's avatar
merlinofchaos committed
66 67 68
    }
  }

69 70
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
71 72 73 74 75 76

    $options['description'] = array('default' => '', 'translatable' => TRUE);

    return $options;
  }

77
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
78
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    $form['description'] = array(
      '#type' => 'textfield',
      '#title' => t('RSS description'),
      '#default_value' => $this->options['description'],
      '#description' => t('This will appear in the RSS feed itself.'),
      '#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().
   */
95
  protected function getChannelElements() {
merlinofchaos's avatar
merlinofchaos committed
96 97 98 99 100 101 102 103 104
    return array();
  }

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

    // Allow substitutions from the first row.
109
    $description = $this->tokenizeValue($description, 0);
merlinofchaos's avatar
merlinofchaos committed
110 111 112 113

    return $description;
  }

114
  public function render() {
115
    if (empty($this->view->rowPlugin)) {
116
      debug('Drupal\views\Plugin\views\style\Rss: Missing row plugin');
117
      return array();
merlinofchaos's avatar
merlinofchaos committed
118 119 120 121 122 123 124 125 126
    }
    $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.
127
    $this->channel_elements = $this->getChannelElements();
merlinofchaos's avatar
merlinofchaos committed
128 129 130 131 132 133 134 135
    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;
136
      $rows .= $this->view->rowPlugin->render($row);
merlinofchaos's avatar
merlinofchaos committed
137 138
    }

139 140 141 142
    $build = array(
      '#theme' => $this->themeFunctions(),
      '#view' => $this->view,
      '#options' => $this->options,
143
      '#rows' => SafeMarkup::set($rows),
144
    );
merlinofchaos's avatar
merlinofchaos committed
145
    unset($this->view->row_index);
146
    return $build;
merlinofchaos's avatar
merlinofchaos committed
147
  }
148

merlinofchaos's avatar
merlinofchaos committed
149
}