node_comments.inc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. if (module_exists('comment')) {
  3. /**
  4. * Plugins are described by creating a $plugin array which will be used
  5. * by the system that includes this file.
  6. */
  7. $plugin = array(
  8. 'single' => TRUE,
  9. 'title' => t('Node comments'),
  10. 'icon' => 'icon_node.png',
  11. 'description' => t('The comments of the referenced node.'),
  12. 'required context' => new ctools_context_required(t('Node'), 'node'),
  13. 'category' => t('Node'),
  14. 'defaults' => array(
  15. 'mode' => variable_get('comment_default_mode', COMMENT_MODE_THREADED),
  16. 'comments_per_page' => variable_get('comment_default_per_page', '50'),
  17. ),
  18. );
  19. }
  20. function ctools_node_comments_content_type_render($subtype, $conf, $panel_args, $context) {
  21. $node = isset($context->data) ? clone $context->data : NULL;
  22. $block = new stdClass();
  23. $block->module = 'comments';
  24. $block->delta = $node->nid;
  25. $block->title = t('Comments');
  26. if (empty($node)) {
  27. $block->content = t('Node comments go here.');
  28. }
  29. else if ($node->comment) {
  30. $block->content = ctools_comment_render($node, $conf);
  31. // Update the history table, stating that this user viewed this node.
  32. node_tag_new($node);
  33. }
  34. return $block;
  35. }
  36. function ctools_node_comments_content_type_edit_form($form, &$form_state) {
  37. $conf = $form_state['conf'];
  38. $form['mode'] = array(
  39. '#type' => 'select',
  40. '#title' => t('Mode'),
  41. '#default_value' => $conf['mode'],
  42. '#options' => _comment_get_modes(),
  43. '#weight' => 1,
  44. );
  45. foreach (_comment_per_page() as $i) {
  46. $options[$i] = t('!a comments per page', array('!a' => $i));
  47. }
  48. $form['comments_per_page'] = array('#type' => 'select',
  49. '#title' => t('Pager'),
  50. '#default_value' => $conf['comments_per_page'],
  51. '#options' => $options,
  52. '#weight' => 3,
  53. );
  54. return $form;
  55. }
  56. function ctools_node_comments_content_type_edit_form_submit($form, &$form_state) {
  57. // Copy everything from our defaults.
  58. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  59. $form_state['conf'][$key] = $form_state['values'][$key];
  60. }
  61. }
  62. function ctools_node_comments_content_type_admin_title($subtype, $conf, $context) {
  63. return t('"@s" comments', array('@s' => $context->identifier));
  64. }
  65. /**
  66. * This function is a somewhat stripped down version of comment_render
  67. * that removes a bunch of cruft that we both don't need, and makes it
  68. * difficult to modify this.
  69. */
  70. function ctools_comment_render($node, $conf) {
  71. $output = '';
  72. if (!user_access('access comments') || !$node->comment) {
  73. return;
  74. }
  75. $mode = $conf['mode'];
  76. $comments_per_page = $conf['comments_per_page'];
  77. $cids = comment_get_thread($node, $mode, $comments_per_page);
  78. $comments = comment_load_multiple($cids);
  79. if ($comments) {
  80. drupal_add_css(drupal_get_path('module', 'comment') . '/comment.css');
  81. comment_prepare_thread($comments);
  82. $build = comment_view_multiple($comments, $node);
  83. $build['pager']['#theme'] = 'pager';
  84. return drupal_render($build);
  85. }
  86. return;
  87. }