views_plugin_style_rss.inc

  1. 3.x plugins/views_plugin_style_rss.inc
  2. 2.x plugins/views_plugin_style_rss.inc

Contains the RSS style plugin.

File

plugins/views_plugin_style_rss.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the RSS style plugin.
  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. function attach_to($display_id, $path, $title) {
  13. $display = $this->view->display[$display_id]->handler;
  14. $url_options = array();
  15. $input = $this->view->get_exposed_input();
  16. if ($input) {
  17. $url_options['query'] = $input;
  18. }
  19. $url_options['absolute'] = TRUE;
  20. $url = url($this->view->get_url(NULL, $path), $url_options);
  21. if ($display->has_path()) {
  22. if (empty($this->preview)) {
  23. drupal_add_feed($url, $title);
  24. }
  25. }
  26. else {
  27. if (empty($this->view->feed_icon)) {
  28. $this->view->feed_icon = '';
  29. }
  30. $this->view->feed_icon .= theme('feed_icon', array('url' => $url, 'title' => $title));
  31. drupal_add_html_head_link(array(
  32. 'rel' => 'alternate',
  33. 'type' => 'application/rss+xml',
  34. 'title' => $title,
  35. 'href' => $url
  36. ));
  37. }
  38. }
  39. function option_definition() {
  40. $options = parent::option_definition();
  41. $options['description'] = array('default' => '', 'translatable' => TRUE);
  42. return $options;
  43. }
  44. function options_form(&$form, &$form_state) {
  45. parent::options_form($form, $form_state);
  46. $form['description'] = array(
  47. '#type' => 'textfield',
  48. '#title' => t('RSS description'),
  49. '#default_value' => $this->options['description'],
  50. '#description' => t('This will appear in the RSS feed itself.'),
  51. '#maxlength' => 1024,
  52. );
  53. }
  54. /**
  55. * Return an array of additional XHTML elements to add to the channel.
  56. *
  57. * @return
  58. * An array that can be passed to format_xml_elements().
  59. */
  60. function get_channel_elements() {
  61. return array();
  62. }
  63. /**
  64. * Get RSS feed description.
  65. *
  66. * @return string
  67. * The string containing the description with the tokens replaced.
  68. */
  69. function get_description() {
  70. $description = $this->options['description'];
  71. // Allow substitutions from the first row.
  72. $description = $this->tokenize_value($description, 0);
  73. return $description;
  74. }
  75. function render() {
  76. if (empty($this->row_plugin)) {
  77. vpr('views_plugin_style_default: Missing row plugin');
  78. return;
  79. }
  80. $rows = '';
  81. // This will be filled in by the row plugin and is used later on in the
  82. // theming output.
  83. $this->namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/');
  84. // Fetch any additional elements for the channel and merge in their
  85. // namespaces.
  86. $this->channel_elements = $this->get_channel_elements();
  87. foreach ($this->channel_elements as $element) {
  88. if (isset($element['namespace'])) {
  89. $this->namespaces = array_merge($this->namespaces, $element['namespace']);
  90. }
  91. }
  92. foreach ($this->view->result as $row_index => $row) {
  93. $this->view->row_index = $row_index;
  94. $rows .= $this->row_plugin->render($row);
  95. }
  96. $output = theme($this->theme_functions(),
  97. array(
  98. 'view' => $this->view,
  99. 'options' => $this->options,
  100. 'rows' => $rows
  101. ));
  102. unset($this->view->row_index);
  103. return $output;
  104. }
  105. }