Need some help with your project? Contact me

How to control the comment form display using the Drupal theme layer

I wrote about a way you can have the logged user's picture displayed in the comment form. But have you ever wondered how you can control more? Like remove the text format description that appears at the bottom of your comment form? Or how you can add some extra html tags around the comment form fields?

There are cases in which you would want to make some alterations to the way the comment form displays. Fortunately, the Drupal theme layer makes it easy for us to hook into the core module and make the necessary changes in a nice clean way.

In order to do this though, you will need to write some lines of code (well, copy paste from here if you want) in your theme template.php file (it should be in your theme root folder). If you don’t have one, just create an empty file and name it like that. Inside, add an opening .php tag (<?php) but not a closing one (this is important).

Also, I recommend installing the Devel module to help you out. Once installed, add the Development block to a sidebar of your site and you have there a list of links with useful information. Also, this module provides some functions that will come in handy during the testing phases of what you want to accomplish. Note, you should not enable this module on a production site as it is strictly for development use only.

Now, to the good stuff. In principle, the comment form that you see on node pages by default does not have a theme implementation already added to the theme registry. But just to make sure, go to a node where you can see the form and click on the theme registry link in the Development block provided by the Devel module.

That will show you the contents of your site’s theme registry (a list of arrays basically). Check the list to see if you can find anything named comment_form. You’ll notice other theme implementations given by the Comment module, but in principle you shouldn’t see one for the actual comment form.

So before you can theme the comment form, you need to register a theme implementation for it. You can do this with hook_theme() by adding the following function to your template.php file (replace yourthemename with the machine readable name of your theme):

function yourthemename_theme($existing, $type, $theme, $path) {
  return array(
    'comment_form' => array(
      'render element' => 'form',

Save the file, clear your cache and if you check the theme registry again, you will find it there in the list (however, since the Devel module will display this list in the default administration theme - usually Seven - you will need to make your theme the default administration theme to view this result).

Now that you have registered a theme implementation for the comment form, you can use it to tap the resources of the theme layer and customize your comment form. To do this, you have to write a custom function using a naming convention you declared in the previous one (like always, replace ‘yourthemename’ with the name of your theme):

function yourthemename_comment_form(&$variables) {

//php magic here    


This is the construct that implements hook_comment_form() (you just made this possible remember?). For fun, take a look what is inside the $variables variable by using the krumo function provided by the Devel module. Just add inside the function one simple line: krumo($variables); and clear the cache.

If you refresh the page where the comment form appears, you will get the contents of the $variables array. You can use that to orient yourself when theming the output of the comment form. And you can do a lot of things.

I will give you but one example of what you can do next: hide the text format description from the bottom of the comment form. A final function to achieve this will look something like this:

function yourthemename_comment_form(&$variables) {

  $comment_form = drupal_render_children($variables['form']);

  return $comment_form; 


This function hides from the renderable element the ‘format’ array before performing the render of the entire ‘form’ array and returning it as output. This of course is a small example, but now that you have the control over what is inside the $variables array, feel free to experiment. But remember, always do these kinds of maneuverings on your development site to avoid problems.

Hope this helps.


Thanks for this great tutorial. It's well written, concise and to the point. A real refresher from what one normal finds (when it's there) on the Drupal site.

Would you be kind enough as to show the code to add so the form is displayed above the comment list and the newest comment immediately below (descending to the oldest)? Provided it's not too much bother of course. Thank you. :)

Its amazing tips.I will use your tips.

Looking for a way to change the value of the submit button, instead of Save, would like it to be: Publish or Post. Any ideas? This is a good start here.

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
$var = 'my variable';