Moving the comment form without hacking core

Published September 30, 2008

One of the most frustrating things about Drupal's current architecture is that comments, and the form to post them, are attached to nodes and don't easily allow you to move them around in your TPL files. There really should be an easier way to just place them anywhere you'd like. Most times this doesn't get in your way, until you want to do some fancy layouts where your content is split into multiple columns on the page. After going round and round with this, I finally happened upon a discussion on Drupal.org that solves this for 5.x rather easily with a snippet of PHP:

if (function_exists('comment_render') && $node->comment) {
   print comment_render($node, $node->cid);
   $node->comment = NULL;
}

Just place this code anywhere in your TPL files where you'd like to move the comments and be happy that you didn't hack core.

Happy Drupaling!

Comments

You can, but I tend to use mini-panels more, and just place them in custom block regions, instead of using full blown panel pages. Plus, for the end node pages, sometimes you just want to move it for one type of content, and not all the others.

- Eric Marden

@venkat-rk

While I agree that you could probably create a single panel that drives EVERY page on your site, sometimes you just need to hand a brother a .tpl.php file, you know?

Submitted by admin on Tue, 09/30/2008 - 16:34

themeing stuff is such a nightmare on drupal. everything else i just love, but getting the html output that you plan to get is a real pita. but i have to say i'm still on D5, ppl told me D6 is much better.
Anyway, thanks for sharing the code.

Submitted by Guest (not verified) on Sat, 11/29/2008 - 03:38

It works if you want to treat comments list and the new comments form as one block. But if you want to insert a region say in between list of comments and new comments form, how would you do that?

Submitted by ajayg (not verified) on Sun, 12/07/2008 - 15:12

ajay,

You'd still use this snippet.

As the original thread states, you'd put this where you want it in your node-TYPE.tpl.php, and the regions should be available on node templates just as easily as page templates.

Correct me if I'm wrong.

For Drupal 6, check out Joon's comment a few lines down:
http://drupal.org/node/122240#comment-707961

Submitted by admin on Wed, 12/10/2008 - 14:01

This is a great hack, but as soon as i make a mistake on the comment form (empty or wrong captcha), the page is displayed again with the hack not working, the form is outside the node again... if (function_exists('comment_render') && $node->comment) returns true... Do you have an idea ?

Submitted by Guest (not verified) on Tue, 01/20/2009 - 12:15

Are there any modules that allow you to make changes to the comment form (number of rows in text area, hiding elements, etc?)

Submitted by Guest (not verified) on Thu, 02/19/2009 - 11:51

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name
CAPTCHA