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.

24 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. Drupal is amzing sir , visit my blog Puppy information

    ReplyDelete
  11. 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
  12. The tutorial on Drupal to print comment anywhere in drupal is not having comprehensive information. Please post regularly with latest in this.

    ReplyDelete
  13. 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
  14. Been looking for such an amazing web journal like this for quite a while.
    www.canvasprints.com

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

    ReplyDelete
  16. It is now very easyfor you to look for a Print Shop Singapore that is justnear your area because now you can use the Internet for that. No need
    for you to explore because in just a few seconds, you will beable to find what you are looking for.

    ReplyDelete
  17. If you are looking for a great quality working Print Shop in Singapore, then you don’t have to look any furtherbecause there is a legit and high quality printing company for your every need.

    ReplyDelete
  18. This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs… Print Shop

    ReplyDelete
  19. If you want a high and excellent quality ofphotos for your advertisement needs or simply for Print Shop

    ReplyDelete
  20. The post is written in very a good manner and it contains many useful information for me. scootersleuth.com/reviews/best-self-balancing-scooters-hoverboards-kids/

    ReplyDelete
  21. Spot lets start on this write-up, I truly think this fabulous website requirements considerably more consideration. I’ll visit here

    ReplyDelete
  22. I’d constantly want to be update on new content on this website, bookmarked! flatbed print

    ReplyDelete