Saturday, February 12, 2011

How to print comment anywhere in Drupal

Drupals standard practice of printing node comments below the node can get a bit stale. Sometimes you may want, or simply need to, print them somewhere else. Wouldn’t be great if comments and the comment form were just variables you could move around?

Well you can—thanks to Drupal 6’s phptemplate_preprocess_page function we can easily store both the comments and the comment form in variables. The only caveat is that you must set the comment reply form to a separate page first, or something might break.

The following snippets store the comment form and comments in variables and allow you print them in page.tpl.php.


// Store comments and the comment form in variables
function phptemplate_preprocess_page(&$vars) {
  $vars['comments'] = $vars['comment_form'] = '';
  if (module_exists('comment') && isset($vars['node'])) {
    $vars['comments'] = comment_render($vars['node']);
    $vars['comment_form'] = drupal_get_form('comment_form',
    array('nid' => $vars['node']->nid));
}
}

// Unset comments from node
function phptemplate_preprocess_node(&$vars) {
$vars['node']->comment = 0;
}
?>



Now you can print $comments and $comment_form anywhere in page.tpl.php, and they will only print when you are viewing a node.

15 comments:

  1. Hello thank you about this preprocess function but when i have pasted in template.php file i have received error could you help me?

    ReplyDelete
  2. http://www.gurutechnoworld.com/on_page_optimization_services.php



    We offer on page optimization services. We have young and dynamic SEO team. All team members are perfect in on page optimization. Our all SEO work is according to Google algorithms

    ReplyDelete
  3. It would be nice to see how you do the same in Drupal7.

    ReplyDelete
  4. this is very useful for me since I'm a new user of drupal, thanks!

    ReplyDelete
  5. VERY informative. I'll be making some juice in the near future. Passing this onto my friends. Thanks.

    ReplyDelete
  6. I recently came across your blog and have been reading along.
    I think I will leave my first comment. I don’t know what to say except that I have enjoyed reading.
    Nice blog. I will keep visiting this blog very often.

    ReplyDelete
  7. Do you need on page optimization service??
    please read our short description below and follow our page url: http://eseobook.com/web-page-optimisation-services.html

    Why We are the best:
    We have smart seo member to do all kind of on page optimization. Our professional team can ready a top quality seo optimized page from the root keyword selection to the latent semantic content writing.

    ReplyDelete
    Replies
    1. Informative article, I really like to keep track of the updates and news. Thanks for the excellent news and topics.

      Delete
  8. I'm impressed. You're truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. I'm saving this for future use.Led Outfitters

    Led Outfitters

    Led outfitters reviews

    led outfitters reviews

    led outfitters complaints

    led outfitters complaints

    led outfitters reviews complaints

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. thanks to Drupal 6’s phptemplate_preprocess_page operate we will simply store each the comments and also the comment type in variables. the sole caveat is that you just should set the comment reply type to a separate page initial, or one thing may break.

    ReplyDelete
  11. The tutorial on Drupal to print comment anywhere in drupal is not having comprehensive information. Please post regularly with latest in this.

    ReplyDelete
  12. This is really very informative blog and it helps me a lot. Thanks for making such a informative blog for us. Keep blogging.
    web development melbourne | website development company melbourne

    ReplyDelete