node_comment_wrapper.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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('Comments and comment form.'),
  10. 'icon' => 'icon_node.png',
  11. 'description' => t('The comments and comment form for 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. /**
  21. * Render the node comments.
  22. */
  23. function ctools_node_comment_wrapper_content_type_render($subtype, $conf, $panel_args, $context) {
  24. $node = isset($context->data) ? clone $context->data : NULL;
  25. $block = new stdClass();
  26. $block->module = 'comments';
  27. $block->delta = $node->nid;
  28. $renderable = array(
  29. '#theme' => 'comment_wrapper__node_' . $node->type,
  30. '#node' => $node,
  31. 'comments' => array(),
  32. 'comment_form' => array(),
  33. );
  34. // Add in the comments.
  35. if (($node->comment_count && user_access('access comments')) || user_access('administer comments')) {
  36. $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
  37. $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
  38. if ($cids = comment_get_thread($node, $mode, $comments_per_page)) {
  39. $comments = comment_load_multiple($cids);
  40. comment_prepare_thread($comments);
  41. $build = comment_view_multiple($comments, $node);
  42. $build['pager']['#theme'] = 'pager';
  43. $renderable['comments'] = $build;
  44. }
  45. }
  46. // Stuff in the comment form.
  47. if ($node->comment == COMMENT_NODE_OPEN) {
  48. if (user_access('post comments')) {
  49. $comment = new stdClass();
  50. $comment->nid = $node->nid;
  51. $comment->pid = NULL;
  52. $form_state = array(
  53. 'ctools comment alter' => TRUE,
  54. 'node' => $node,
  55. 'build_info' => array(
  56. 'args' => array(
  57. $comment,
  58. ),
  59. ),
  60. );
  61. $renderable['comment_form'] = drupal_build_form('comment_node_' . $node->type . '_form', $form_state);
  62. }
  63. else if (!empty($conf['anon_links'])) {
  64. $renderable['comment_form'] = theme('comment_post_forbidden', array('node' => $node));
  65. }
  66. }
  67. $block->content = drupal_render($renderable);
  68. return $block;
  69. }
  70. /**
  71. * Returns an edit form for the comment wrapper.
  72. */
  73. function ctools_node_comment_wrapper_content_type_edit_form($form, &$form_state) {
  74. $conf = $form_state['conf'];
  75. $form['mode'] = array(
  76. '#type' => 'select',
  77. '#title' => t('Mode'),
  78. '#default_value' => $conf['mode'],
  79. '#options' => _comment_get_modes(),
  80. '#weight' => 1,
  81. );
  82. foreach (_comment_per_page() as $i) {
  83. $options[$i] = t('!a comments per page', array('!a' => $i));
  84. }
  85. $form['comments_per_page'] = array('#type' => 'select',
  86. '#title' => t('Pager'),
  87. '#default_value' => $conf['comments_per_page'],
  88. '#options' => $options,
  89. '#weight' => 3,
  90. );
  91. return $form;
  92. }
  93. /**
  94. * Submit handler for the comment wrapper settings form.
  95. */
  96. function ctools_node_comment_wrapper_content_type_edit_form_submit($form, &$form_state) {
  97. // Copy everything from our defaults.
  98. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  99. $form_state['conf'][$key] = $form_state['values'][$key];
  100. }
  101. }
  102. /**
  103. * Returns the administrative title.
  104. */
  105. function ctools_node_comment_wrapper_content_type_admin_title($subtype, $conf, $context) {
  106. return t('Comments and comment form');
  107. }