views_plugin_style_rss.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_style_rss.
  5. */
  6. /**
  7. * Default style plugin to render an RSS feed.
  8. *
  9. * @ingroup views_style_plugins
  10. */
  11. class views_plugin_style_rss extends views_plugin_style {
  12. /**
  13. *
  14. */
  15. public function attach_to($display_id, $path, $title) {
  16. $display = $this->view->display[$display_id]->handler;
  17. $url_options = array();
  18. $input = $this->view->get_exposed_input();
  19. if ($input) {
  20. $url_options['query'] = $input;
  21. }
  22. $url_options['absolute'] = TRUE;
  23. $url = url($this->view->get_url(NULL, $path), $url_options);
  24. if ($display->has_path()) {
  25. if (empty($this->preview)) {
  26. drupal_add_feed($url, $title);
  27. }
  28. }
  29. else {
  30. if (empty($this->view->feed_icon)) {
  31. $this->view->feed_icon = '';
  32. }
  33. $this->view->feed_icon .= theme('feed_icon', array('url' => $url, 'title' => $title));
  34. drupal_add_html_head_link(array(
  35. 'rel' => 'alternate',
  36. 'type' => 'application/rss+xml',
  37. 'title' => $title,
  38. 'href' => $url,
  39. ));
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function option_definition() {
  46. $options = parent::option_definition();
  47. $options['description'] = array('default' => '', 'translatable' => TRUE);
  48. return $options;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function options_form(&$form, &$form_state) {
  54. parent::options_form($form, $form_state);
  55. $form['description'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('RSS description'),
  58. '#default_value' => $this->options['description'],
  59. '#description' => t('This will appear in the RSS feed itself.'),
  60. '#maxlength' => 1024,
  61. );
  62. }
  63. /**
  64. * Return an array of additional XHTML elements to add to the channel.
  65. *
  66. * @return
  67. * An array that can be passed to format_xml_elements().
  68. */
  69. public function get_channel_elements() {
  70. return array();
  71. }
  72. /**
  73. * Return an atom:link XHTML element to add to the channel to comply with
  74. * the RSS 2.0 specification.
  75. *
  76. * @see http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html
  77. *
  78. * @return
  79. * An array that can be passed to format_xml_elements().
  80. */
  81. public function get_channel_elements_atom_link() {
  82. $url_options = array('absolute' => TRUE);
  83. $input = $this->view->get_exposed_input();
  84. if ($input) {
  85. $url_options['query'] = $input;
  86. }
  87. $url = url($this->view->get_url(), $url_options);
  88. return array(
  89. array(
  90. 'namespace' => array('xmlns:atom' => 'http://www.w3.org/2005/Atom'),
  91. 'key' => 'atom:link',
  92. 'attributes' => array(
  93. 'href' => $url,
  94. 'rel' => 'self',
  95. 'type' => 'application/rss+xml',
  96. ),
  97. ),
  98. );
  99. }
  100. /**
  101. * Get RSS feed description.
  102. *
  103. * @return string
  104. * The string containing the description with the tokens replaced.
  105. */
  106. public function get_description() {
  107. $description = $this->options['description'];
  108. // Allow substitutions from the first row.
  109. $description = $this->tokenize_value($description, 0);
  110. return $description;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function render() {
  116. if (empty($this->row_plugin)) {
  117. vpr('views_plugin_style_default: Missing row plugin');
  118. return;
  119. }
  120. $rows = '';
  121. // This will be filled in by the row plugin and is used later on in the
  122. // theming output.
  123. $this->namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/');
  124. // Fetch any additional elements for the channel and merge in their
  125. // namespaces.
  126. $this->channel_elements = array_merge(
  127. $this->get_channel_elements(),
  128. $this->get_channel_elements_atom_link()
  129. );
  130. foreach ($this->channel_elements as $element) {
  131. if (isset($element['namespace'])) {
  132. $this->namespaces = array_merge($this->namespaces, $element['namespace']);
  133. }
  134. }
  135. foreach ($this->view->result as $row_index => $row) {
  136. $this->view->row_index = $row_index;
  137. $rows .= $this->row_plugin->render($row);
  138. }
  139. $output = theme($this->theme_functions(),
  140. array(
  141. 'view' => $this->view,
  142. 'options' => $this->options,
  143. 'rows' => $rows,
  144. ));
  145. unset($this->view->row_index);
  146. return $output;
  147. }
  148. }