views_plugin_row_node_rss.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Contains the node RSS row style plugin.
  5. */
  6. /**
  7. * Plugin which performs a node_view on the resulting object
  8. * and formats it as an RSS item.
  9. */
  10. class views_plugin_row_node_rss extends views_plugin_row {
  11. // Basic properties that let the row style follow relationships.
  12. var $base_table = 'node';
  13. var $base_field = 'nid';
  14. // Stores the nodes loaded with pre_render.
  15. var $nodes = array();
  16. function option_definition() {
  17. $options = parent::option_definition();
  18. $options['item_length'] = array('default' => 'default');
  19. $options['links'] = array('default' => FALSE, 'bool' => TRUE);
  20. return $options;
  21. }
  22. /**
  23. * Override init function to convert fulltext view-mode to full.
  24. */
  25. function init(&$view, &$display, $options = NULL) {
  26. parent::init($view, $display, $options);
  27. if ($this->options['item_length'] == 'fulltext') {
  28. $this->options['item_length'] = 'full';
  29. }
  30. }
  31. function options_form(&$form, &$form_state) {
  32. parent::options_form($form, $form_state);
  33. $form['item_length'] = array(
  34. '#type' => 'select',
  35. '#title' => t('Display type'),
  36. '#options' => $this->options_form_summary_options(),
  37. '#default_value' => $this->options['item_length'],
  38. );
  39. $form['links'] = array(
  40. '#type' => 'checkbox',
  41. '#title' => t('Display links'),
  42. '#default_value' => $this->options['links'],
  43. );
  44. }
  45. /**
  46. * Return the main options, which are shown in the summary title.
  47. */
  48. function options_form_summary_options() {
  49. $entity_info = entity_get_info('node');
  50. $options = array();
  51. if (!empty($entity_info['view modes'])) {
  52. foreach ($entity_info['view modes'] as $mode => $settings) {
  53. $options[$mode] = $settings['label'];
  54. }
  55. }
  56. $options['title'] = t('Title only');
  57. $options['default'] = t('Use site default RSS settings');
  58. return $options;
  59. }
  60. function summary_title() {
  61. $options = $this->options_form_summary_options();
  62. return check_plain($options[$this->options['item_length']]);
  63. }
  64. function pre_render($values) {
  65. $nids = array();
  66. foreach ($values as $row) {
  67. $nids[] = $row->{$this->field_alias};
  68. }
  69. if (!empty($nids)) {
  70. $this->nodes = node_load_multiple($nids);
  71. }
  72. }
  73. function render($row) {
  74. // For the most part, this code is taken from node_feed() in node.module
  75. global $base_url;
  76. $nid = $row->{$this->field_alias};
  77. if (!is_numeric($nid)) {
  78. return;
  79. }
  80. $display_mode = $this->options['item_length'];
  81. if ($display_mode == 'default') {
  82. $display_mode = variable_get('feed_item_length', 'teaser');
  83. }
  84. // Load the specified node:
  85. $node = $this->nodes[$nid];
  86. if (empty($node)) {
  87. return;
  88. }
  89. $item_text = '';
  90. $uri = entity_uri('node', $node);
  91. $node->link = url($uri['path'], $uri['options'] + array('absolute' => TRUE));
  92. $node->rss_namespaces = array();
  93. $node->rss_elements = array(
  94. array(
  95. 'key' => 'pubDate',
  96. 'value' => gmdate('r', $node->created),
  97. ),
  98. array(
  99. 'key' => 'dc:creator',
  100. 'value' => format_username($node),
  101. ),
  102. array(
  103. 'key' => 'guid',
  104. 'value' => $node->nid . ' at ' . $base_url,
  105. 'attributes' => array('isPermaLink' => 'false'),
  106. ),
  107. );
  108. // The node gets built and modules add to or modify $node->rss_elements
  109. // and $node->rss_namespaces.
  110. $build_mode = $display_mode;
  111. $build = node_view($node, $build_mode);
  112. unset($build['#theme']);
  113. if (!empty($node->rss_namespaces)) {
  114. $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
  115. }
  116. elseif (function_exists('rdf_get_namespaces')) {
  117. // Merge RDF namespaces in the XML namespaces in case they are used
  118. // further in the RSS content.
  119. $xml_rdf_namespaces = array();
  120. foreach (rdf_get_namespaces() as $prefix => $uri) {
  121. $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
  122. }
  123. $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
  124. }
  125. // Hide the links if desired.
  126. if (!$this->options['links']) {
  127. hide($build['links']);
  128. }
  129. if ($display_mode != 'title') {
  130. // We render node contents and force links to be last.
  131. $build['links']['#weight'] = 1000;
  132. $item_text .= drupal_render($build);
  133. }
  134. $item = new stdClass();
  135. $item->description = $item_text;
  136. $item->title = $node->title;
  137. $item->link = $node->link;
  138. $item->elements = $node->rss_elements;
  139. $item->nid = $node->nid;
  140. return theme($this->theme_functions(), array(
  141. 'view' => $this->view,
  142. 'options' => $this->options,
  143. 'row' => $item
  144. ));
  145. }
  146. }