Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal: Customising the comments form title

Parent Feed: 

As we move into 2011, I have been doing a lot of work with comments in Drupal lately (Drupal 6 to be specific). One thing I have always wanted to know how to do, but never got round to investigating until recently, was the ability to customise the "Post new comment" tagline at the top of the comment form.

Anyone with a passing familarity of the inner working of Drupal would assume that you need to use a "_form_alter" (or Form Alter in English) hook to modify this piece of text on the form. That was certainly my first assumption, and funnily enough, it proved to be incorrect. I spent a fair bit of time with my head stuck in XDebug in Eclipse (the FiveRDesign IDE of choice, for lack of a better option) following the different steps through and changing different variables but nothing would affect the title.

After much digging around Drupal.org unsuccessfully for help on this matter I stumbled across a post which thankfully gave the simple answer (I unfortunately can't find the original post now to give them credit). What is required to override the comment form's title is actually a preprocess function, specifically, preprocess_box. Boxes are one layer of the Drupal theme API I haven't touched before so I was a little bit surprised about this.

All that is required is the following code:

function module_name_theme() {
	return array(
    	    'preprocess_box' => array(
      		'arguments' => array('vars' => array(), 'hook' => NULL),
    	    )
	);
}

function module_name_preprocess_box(&$vars, $hook) {
	switch ($vars['title']) {
		case 'Post new comment':
			$vars['title'] = 'Your new title';
		break;
	}
}

And hey presto, a customised comment form title. This could probably also be achieved with a custom template or overriding the theme function for the comment form if you wanted to take the forms customisation further, but if you just want to alter the title, knock up a quick custom module with this hook in it (and with a _theme hook to register your theme_preprocess_box function) and your comment form will have a custom title in no time.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web