node_comment_wrapper.inc 3.6 KB

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