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.
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.

Comments
How to add a dropdownlist?
Hi,
Thank you for this article. Can you tell me if it's possible to add a new field from CCK? I need to add a dropdown list.
Thank you in advance.
Regards, Julien
Take a look at the Node Comments module
Hi Julien,
In Drupal 6, comments are not nodes, and so you can not add CCK fields to them. In Drupal 7, comments are 'fieldable' so you will be able to extend them in this way.
However, for Drupal 6 you may want to check out the Node Comments module, which you can use instead of the core comments module, and allows you to designate a content type that can be used for comments. This should allow you to use all the goodness of CCK fields in your comment form.
Customise for a particular node or content type
Hello,
Can you tell me if it's possible to customise the override function in order to use it only for a particular node or content type?
Thank you.
Once way you could do this is
Once way you could do this is by using an if statement in your theme function to check the node type, and only make modifications if it is a certain type. However, this information isn't readily available in the theme function, so you need to look it up first. Adjust your theme_comment_form function as follows:
function mytheme_comment_form($form) { // Look up the node type. $nid = $form['nid']['#value']; $node = node_load(array('nid' => $nid)); // Only edit the comment form if the node type is 'story'. if ($node->type == 'story') { // Your form customisations here } return drupal_render($form); }Obviously, you can change 'story' to the name of the content type you are interested in modifying.
Great article helped me a lot
Great article helped me a lot
Can't get it to work with my Zen subtheme
Hi, thanks for the great article. I followed along step by step, but it doesn't seem to be having any impact at all on the way my contact form is beind displayed. I'm working with a sub theme of Zen (my subtheme is called ICH).
I tried to do something similar to this yesterday -- to add custom regions to my site for blocks. Modifying the template.php file didn't work for that, either, but modifying the zen.info (or, in my case, the ich.info) file did work. Does that mean I'm missing a setting to allow these kind of overrides to work? Or could it be something else?
Any help is greatly appreciated. I'm new to Drupal, and getting my head wrapped around all the different ways to modify output has been slow-going.
Did you clear your theme registry?
Hi Nathaniel
Did you clear your theme registry after making the changes (specifically, after adding the region to your theme's .info file)?
If you have the devel module installed, you can do this at:
Admin -> Flush all caches -> Theme registry
Thanks. I'm such a n00b.
Hi Tom, Right before you posted this, I read about clearing the cache in Drupal. Did that, and the edits in your example came through. Thanks very much, for your time, and for sharing your expertise. Its been a big help!
Sorry, one more question.
So I was able to change "homepage" to website, etc... But do you happen to know how I would remove the hints and the line "More information about formatting options" from beneath the large comment box? I'm using TINYMce (although I've disabled it for the comments form by modifying its JavaScript).
Is it similar to how you modified the first batch of elements? Or would that particular element be something different altogether?
Take a look at Better Formats
Hi Nathaniel,
You should take a look at the Better Formats module. Amongst other things, this module introduces a new permission which allows you to hide the more the format tips link.
Variables in template?
Thanks fora great writeup. I am able to create a new template comment-form.tpl.php based on your writup. Thank you.
Now I am stumped. How can I know which variables are available for me in this template? There are some hidden variables as well like "form_build_id" which need correct value for form to validated. Where can I get this list?
devel::dsm()
To inspect the available variables in the comment-form.tpl.php file, you should install the devel module, and then use the dsm() function. In template.php, create a new preprocess function for this purpose. Something like this:
mytheme_preprocess_comment_form($vars) { dsm($vars); }That should show you the full content of $vars. Each key in the $vars array is available as a variable in the template file. So, for example if there is a $vars['somekey'], then $somekey is available for use in the template file. Note that you will need to clear your theme registry after adding the preprocess function to template.php.
How to add user Avatars into the comment form?
Hi, Thanks for the lovely write-up. Very useful!
I'm trying to display the avatar of the currently logged-in user next to the comment form. (kinda like what Facebook used to have, and it was very inviting to quickly post comments). If the user is anonymous, or does not have an avatar, then the default avatar should be displayed.
I searched and searched for such snippet, but no result. Quite a few people would love such feature, but there is no write-up for people who are not coding gurus.
Could you possibly help? Thank you in advance!
Post new comment