views_plugin_row_comment_rss.inc 4.3 KB

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