How to customise the comment form in Drupal 6
Most forms in drupal can be customised, or 'themed' by writing a few lines of code in your theme's template.php file. However, the comment form is one of a few exceptions where this doesn't work - not with a little extra magic anyway. As always, there is more than one possible approach, some of them are good, some bad. In this article, we'll be doing it the right way - The Drupal way.
I found myself wanting to do this yet again, on my own comment forms this morning and just couldn't remember how or where I had done it before. So, this post will serve as a reference for myself, and of course a valuable resource for all you Drupal themers.
A little inside information...
if you were to look inside comment.module which can be found in your Drupal installation at /modules/comments/comment.module you would probably notice the comment_form() function which is responsible for actually building the comment form using the Forms API. However, unlike other modules that build forms in a similar way, what the comment module doesn't do is register a corresponding theme_ function for the form, which you need in order to be able to control it's output using a theme_ function.
Tip: To get a better understanding of how forms are built and themed, have a read of the Forms API Quickstart Guide, particularity relevant to this discussion is section 2 under the heading 'Theming forms'.
Registering a theme function
So, to be able to theme the comment form, we need to register our own theme function for this form. If your theme is a subtheme of the excellent zen theme, you can do this in your template,php file like so:
/**
* Implementation of hook_theme().
*/
function mytheme_theme(&$existing, $type, $theme, $path) {
$hooks = zen_theme($existing, $type, $theme, $path);
// Add your theme hooks like this:
/*
$hooks['hook_name_here'] = array( // Details go here );
*/
$hooks['comment_form'] = array(
'arguments' => array('form' => NULL),
// Note: by uncommenting the following line, you can also use a
// template file named comment-form.tpl.php to control the
// output of the form.
/*'template' => 'comment-form', */
);
return $hooks;
}
Note: you must rename the theme function so it matches the name of your theme (replace mytheme with the name of your theme).
If your theme is not a subtheme of zen, your function will be a little simpler and should look more like this:
/**
* Implementation of hook_theme().
*/
function mytheme_theme(){
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
The theme function itself
You can theme create the theme_comment_form function, again in your theme's template.php file. In my case, I wanted to rename the 'Your name' field to just 'Name', the 'Homepage' field to 'Website' and 'Comment' to 'Your message'. I also wanted to add a little help message to the homepage field, as I don't think it is clear to everybody what the field is for. I also removed the preview button as I don't feel it's needed.
/**
* Theme the output of the comment_form.
*
* @param $form
* The form that is to be themed.
*/
function mytheme_comment_form($form) {
// Rename some of the form element labels.
$form['name']['#title'] = t('Name');
$form['homepage']['#title'] = t('Website');
$form['comment_filter']['comment']['#title'] = t('Your message');
// Add some help text to the homepage field.
$form['homepage']['#description'] = t('If you have your own website, enter its address here and we will link to it for you. (please include http://).');
$form['homepage']['#description'] .= '<br/>'. t('eg. http://www.kirkdesigns.co.uk');
// Remove the preview button
$form['preview'] = NULL;
return drupal_render($form);
}
Altering the 'Post new comment' heading
On top of that, I also wanted to change the title of the comments form which by default says 'Post new comment'. This is actually generated elsewhere - in the function theme_box. Because this is already created by a theme_ function, there is no need for us to register our own theme_ function so we can go straight in and use use Drupal 6's preprocess function (still in your theme's template.php) file to modify the template variables before they are passed into the theme_box function:
function mytheme_preprocess_box(&$vars, $hook) {
switch($vars['title']) {
case 'Post new comment':
$vars['title'] = t('Leave a comment or suggestion...');
}
}
On a similar note, you might be interested in my article on Customizing the search box in Drupal 6.
LATEST ARTICLES
TODAY'S MOST POPULAR
-
8th Aug 08
-
4th Sep 08
-
14th Nov 09
MOST POPULAR ARTICLES
RECENT COMMENTS
-
1 day 23 hours ago
-
4 days 12 hours ago
-
4 days 18 hours ago
-
5 days 13 min ago
-
5 days 16 hours ago

Comments
17th Nov 2009, 6:00am
I would argue that the more semantic place to alter form text is via <a href="http://api.drupal.org/api/function/hook_form_alter/6">hook_form_alter</a> or <a href="http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6">hook_form_FORM_ID_alter</a>. This can also be done in just one step instead of two.
17th Nov 2009, 9:42am
@dalin: In many cases I would agree with you, however in this particular case the main reason I wanted to alter the form was to do with theming reasons - I needed to shorten the length of the form element labels so that they would fit inline with the form elements properly. Since the change was related to this particular theme, I found it more appropriate to make the change at the theme layer.
18th Nov 2009, 4:00am
Hi tom, I am not using zen theme. Therefore I think I didn't need to use the code you had put inside "registering the theme". I tried your code and placed only the code for "altering the form itself". It is not working. How can I "register my theme" (for non-zen theme users)?
18th Nov 2009, 4:00am
I got an answer to my question:
/** * Register the theme edit for form */ function mytheme_theme() { return array( 'comment_form' => array( // Forms always take the form argument. 'arguments' => array('form' => NULL), ), ); }18th Nov 2009, 8:07am
@chanaky: You are absolurely right. I have updated the article to include the theme registering function fot non zen subtheme users.Thanks.
28th Nov 2009, 6:00am
This should prove handy next time I'm playing around with Drupal. Thanks a lot man!
16th Dec 2009, 1:00am
There are always great people like you out there to show us the way to do things better. Thanks,Jesse Chen
23rd Feb 2010, 11:00pm
I would like to add an anchor tag to the 'Post New Comment' header so that users will be able to click a link at the top of the page to shoot them down to the comment form.
Do you know how could I go about doing this?
Thanks so much
24th Feb 2010, 9:28am
Sure... Where I have put:
$vars['title'] = t('Leave a comment or suggestion...');You should put:
$path = isset($_GET['q']) ? $_GET['q'] : '<front>'; $vars['title'] = l(t('Post new comment'), $path, array('attributes' => array('name' => 'name-of-anchor')));24th Feb 2010, 10:00pm
Thanks for your super-quick reply. However, I implemented the code and nothing changed. I even inputed your original mytheme_preprocess_box function and nothing changed. Even more strange, every other function in my template.php file works except for this one.
Do you have any idea of why this would be? If not, thanks so much for your help anways
24th Feb 2010, 10:00pm
Stupid mistake, I forgot to clear my cache. Your code works perfectly :)
28th Feb 2010, 9:00pm
Great post, thank you very much for taking the time to share with those who are starting on the Drupal.
Please share your thoughts, comments and suggestions...