Jump to Navigation

How to customise the comment form in Drupal 6

Image to accompany story

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.

Comments

Anonymous's picture

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.

tom's picture

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

Anonymous's picture

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)?

Anonymous's picture

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),
     ),
  );
}

 

tom's picture

@chanaky: You are absolurely right. I have updated the article to include the theme registering function fot non zen subtheme users.Thanks.

Anonymous's picture

This should prove handy next time I'm playing around with Drupal. Thanks a lot man!

Anonymous's picture

There are always great people like you out there to show us the way to do things better. Thanks,Jesse Chen

Please share your thoughts, comments and suggestions...

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
If you have your own website, enter its address here and we will link to it for you. (please include http://).
eg. http://www.kirkdesigns.co.uk