views_plugin_row_comment_rss.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. * Contains the comment RSS row style plugin.
  5. */
  6. /**
  7. * Plugin which formats the comments as RSS items.
  8. */
  9. class views_plugin_row_comment_rss extends views_plugin_row {
  10. var $base_table = 'comment';
  11. var $base_field = 'cid';
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['item_length'] = array('default' => 'default');
  15. $options['links'] = array('default' => FALSE, 'bool' => TRUE);
  16. return $options;
  17. }
  18. function options_form(&$form, &$form_state) {
  19. parent::options_form($form, $form_state);
  20. $form['item_length'] = array(
  21. '#type' => 'select',
  22. '#title' => t('Display type'),
  23. '#options' => $this->options_form_summary_options(),
  24. '#default_value' => $this->options['item_length'],
  25. );
  26. $form['links'] = array(
  27. '#type' => 'checkbox',
  28. '#title' => t('Display links'),
  29. '#default_value' => $this->options['links'],
  30. );
  31. }
  32. function pre_render($result) {
  33. $cids = array();
  34. $nids = array();
  35. foreach ($result as $row) {
  36. $cids[] = $row->cid;
  37. }
  38. $this->comments = comment_load_multiple($cids);
  39. foreach ($this->comments as &$comment) {
  40. $comment->depth = count(explode('.', $comment->thread)) - 1;
  41. $nids[] = $comment->nid;
  42. }
  43. $this->nodes = node_load_multiple($nids);
  44. }
  45. /**
  46. * Return the main options, which are shown in the summary title
  47. *
  48. * @see views_plugin_row_node_rss::options_form_summary_options()
  49. * @todo: Maybe provide a views_plugin_row_rss_entity and reuse this method
  50. * in views_plugin_row_comment|node_rss.inc
  51. */
  52. function options_form_summary_options() {
  53. $entity_info = entity_get_info('node');
  54. $options = array();
  55. if (!empty($entity_info['view modes'])) {
  56. foreach ($entity_info['view modes'] as $mode => $settings) {
  57. $options[$mode] = $settings['label'];
  58. }
  59. }
  60. $options['title'] = t('Title only');
  61. $options['default'] = t('Use site default RSS settings');
  62. return $options;
  63. }
  64. function render($row) {
  65. global $base_url;
  66. $cid = $row->{$this->field_alias};
  67. if (!is_numeric($cid)) {
  68. return;
  69. }
  70. $item_length = $this->options['item_length'];
  71. if ($item_length == 'default') {
  72. $item_length = variable_get('feed_item_length', 'teaser');
  73. }
  74. // Load the specified comment and its associated node:
  75. $comment = $this->comments[$cid];
  76. if (empty($comment) || empty($this->nodes[$comment->nid])) {
  77. return;
  78. }
  79. $item_text = '';
  80. $uri = entity_uri('comment', $comment);
  81. $comment->link = url($uri['path'], $uri['options'] + array('absolute' => TRUE));
  82. $comment->rss_namespaces = array();
  83. $comment->rss_elements = array(
  84. array(
  85. 'key' => 'pubDate',
  86. 'value' => gmdate('r', $comment->created),
  87. ),
  88. array(
  89. 'key' => 'dc:creator',
  90. 'value' => format_username($comment),
  91. ),
  92. array(
  93. 'key' => 'guid',
  94. 'value' => 'comment ' . $comment->cid . ' at ' . $base_url,
  95. 'attributes' => array('isPermaLink' => 'false'),
  96. ),
  97. );
  98. // The comment gets built and modules add to or modify
  99. // $comment->rss_elements and $comment->rss_namespaces.
  100. $build = comment_view($comment, $this->nodes[$comment->nid], 'rss');
  101. unset($build['#theme']);
  102. if (!empty($comment->rss_namespaces)) {
  103. $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $comment->rss_namespaces);
  104. }
  105. // Hide the links if desired.
  106. if (!$this->options['links']) {
  107. hide($build['links']);
  108. }
  109. if ($item_length != 'title') {
  110. // We render comment contents and force links to be last.
  111. $build['links']['#weight'] = 1000;
  112. $item_text .= drupal_render($build);
  113. }
  114. $item = new stdClass();
  115. $item->description = $item_text;
  116. $item->title = $comment->subject;
  117. $item->link = $comment->link;
  118. $item->elements = $comment->rss_elements;
  119. $item->cid = $comment->cid;
  120. return theme($this->theme_functions(), array(
  121. 'view' => $this->view,
  122. 'options' => $this->options,
  123. 'row' => $item
  124. ));
  125. }
  126. }