Modifying the contact form in Drupal: how to add a field

Please notice: This article is for Drupal 6.

It would be great if we could add and remove fields to the contact form the same way we add them to content types using CCK, don't you think? But at this moment, this is not yet possible. As of today, there's no module that let us do it using the control panel. We need to do it "geek way", writing code.

The good news is that, as usual, we don't need to hack Drupal's core. We just use hook_form_alter to make the trick.

OK, let's do it.

Step 1: You need an empty module

As explained here, you need a place to put your custom code. A good place for your "code tricks" can be an empty module where you put all your custom stuff. You can go here for more info on creating an empty module.

Step 2: Modify the form

For example, imagine you want to add a selector field to your site-wide contact form. For example, you want to ask user's favourite color. This would be the code:

function [--foo--]_form_contact_mail_page_alter(&$form, $form_state) {
// Options on our new selector box
	$options = array(
		'red' => t('Red'),
		'green' => t('Green'),
		'blue' => t('Blue'),
		'none' => t('I don\'t bother')
	);
	$form['color'] = array(
		'#title' => t('Favourite color'),
		'#type' => 'select',
		'#required' => true,
		'#options' => $options,
		'#default_value' => 'none'
	);

// Resorting the whole form, so that our new field appears where we want to.
	$order = array ('name', 'mail', 'subject',
 'color', 'cid', 'message', 'copy', 'submit');
	foreach($order as $key => $field) {
		$form[$field]['#weight'] = $key;
	}
}

That's it, we modified the form. Remember to change [--foo--] by your custom module's name, and please notice we use the t() function to make the text of the new field translatable.

If you want more info on forms manipulation, check the Drupal Forms API quickstart guide.

Now, we must modify the output so we receive the data of this new field when someone sends us a message.

Step 3: Modify the email we receive

This is even easier. We use the mail_alter hook, being careful to alter only the contact form mail, as this hook is used for every mail the system sends:

function [--foo--]_mail_alter(&$message) {
// We have to be careful to alter only the contact form mail,
// as this hook is used for every mail the system sends.
	if ($message['id'] == 'contact_page_mail') {
		// We're gonna insert 'Favourite color' before message body.
		$message['body'][2] = $message['body'][1];
		$options = array(
		'red' => t('Red'),
		'green' => t('Green'),
		'blue' => t('Blue'),
		'none' => t('I don\'t bother')
		);
		$message['body'][1] = t('Favour').': '
.$options[$message['params']['color']];
	}
}

Save and enjoy. You did it. You modified the site-wide contact form in Drupal. No big deal, isn't it?

Comments
Cheap Web Design London (not verified) on Sun, 05/02/2010 - 16:07

This was the one I was looking,
Adding extrafileds and posting

great and thank you

handsofaten (not verified) on Mon, 05/03/2010 - 19:52

Thanks for the tip, this is very useful. One note. When I first implemented this, the site had the Name field before the form instructions that should go at the top. I fixed this by numbering the keys in the order array starting at 1, eg:

$order = array (1 => 'name', 2 => 'mail', 3 => 'phone', 4 => 'subject', 5 => 'message', 6 => 'copy', 7 => 'submit');

Webmaster Mexico (not verified) on Fri, 05/07/2010 - 12:34

Excellent work, thanks!

Anónimo (not verified) on Wed, 06/09/2010 - 16:19

Sorry, I'm not particularly good at php - would it be possible to give an example in doing this with a text field - for eg. An additional telephone number field...

james (not verified) on Fri, 07/30/2010 - 12:24

This is great! Helped alot, good job.

Anónimo (not verified) on Tue, 08/24/2010 - 17:47

Thanks very much, I've been looking for it and nobody had a clue. You saved my day.

Diazepmonline46hrv (not verified) on Wed, 09/01/2010 - 01:26

http://www.livescribe.com/forums/member.php?u=8059&uz17=3 [url=http://www.livescribe.com/forums/member.php?u=8058&uz17=3]generic viagra[/url] buy tramadol viagra [url="http://www.livescribe.com/forums/member.php?u=8054&uz17=3"]cheap fioricet[/url] [LINK http://www.livescribe.com/forums/member.php?u=8060&uz17=3]generic cialis[/LINK] naor

Anónimo (not verified) on Sat, 09/04/2010 - 05:54

Try http://drupal.org/project/contact_field module instead.

Thanks

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <h2> <h3> <blockquote>
  • Lines and paragraphs break automatically.