cool tech graphics

How to Add a Field to the Drupal Contact form

Filed under:

Ever wish you could use the existing Drupal Contact module as your sitewide contact form and just add a field or two? You can and it's really easy. All you need to do is create a new module and add the fields via the hook_form_alter() function. Here's an example of how we do it on

 * Implementation of hook_form_alter().
 * @see
 * We add a few custom fields to the default Drupal contact form
function mtmcontact_form_contact_mail_page_alter(&$form, $form_state) {
	$form['company'] = array(
		'#title' => t('Your company'),
		'#type' => 'textfield',
		'#required' => true,
	$form['phone'] = array(
		'#title' => t('Your phone'),
		'#type' => 'textfield',
	// reorder the elements in the form to include the elements being inserted
	$order = array('contact_information', 'name', 'company', 'phone', 'mail', 'subject', 'message', 'copy', 'submit');
	foreach($order as $key => $field) {
		$form[$field]['#weight'] = $key;

The second part keeps the fields in an explicit order and includes references to the form elements that are already created by the Contact module. To change the order, simply modify the array.

Want more awesome content like this? Check out our top 20 Drupal tips of all time!

Date posted: January 27, 2009


You'll have to use hook_alter_message() to actually send the values of your new fields via email.

Muy buen tutorial, felicidades

Where in drupal, i mean in which file I have to do this changes. I am using drupal version 6. So please help me out.

Sorry for not putting home page, because i have my site on localhost. Trying to learn drupal.


I have modified the default contact form with some additional fields
This information was really useful

As we get further along in the Drupal major revision timeline, it's worth noting that this tutorial is for Drupal 6.

Someone knows how to add a javascript function (es. onchange) to a the SELECT tag of Category filed in Drupal 6?

I'm trying with #attributes but seems not work..

onchange is a bit old-fashioned. I would recommend using jQuery to bind the change event. See for some examples. This code would go in a javascript file, not in your form_alter.

WHat D version is this for? I'm just trying to add a "phone" field in D7 (first project since moveing from D6) and it won't work using hook_form_alter!

This code was written for Drupal 6. It's pretty obsolete now, I would recommend trying the webform module instead.

how can I remove the message text area from contact form?
I need just: email, name, phone, city and drop down list.


Since this article is for drupal 6, how do I add another field in drupal 7?

it works well to add field for this form .but how can i save the field's data to database ?

I need to implement "Send yourself a copy." feature as we would find in the default contact form on Drupal 7 to new form I setup with Webforms module. Can someone be able to help me with this.

FYI, in drupal 7 the hook changes, you need to use:


So your function would look like:

function yourmodule_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
// place code here.

Use something like this to send company and phone with mail message:

* Implements hook_mail().
function YOURMODULENAME_mail_alter(&$message) {
if ($message['id'] == 'contact_page_mail' ||
$message['id'] == 'contact_page_autoreply') {
$message['body'][] = t('company') . ': "' . $message['params']['company'] . '"';
$message['body'][] = t('phone') . ': "' . $message['params']['phone'] . '"';

. . .

I am using CCK email field that redirect to drupal default contact form. In that case the form changed from contact_page_mail to something different. How to find the form ID. I have just changed the code mentioned for default contact form to CCK email filed contact. everything is working fine but the information which is filled in the new fields is not sent on email. I am not able to get the exact form ID.

Can you tell me how find the form ID?

So how do I add the additional fields to the message text

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Metal Toad is an Advanced AWS Consulting Partner. Learn more about our AWS Managed Services

About the Author

Joaquin Lippincott, CEO

Joaquin is a 20+ year technology veteran helping to lead businesses in the move to the Cloud. He frequently speaks on panels about the future of tech ranging from IoT and Machine Learning to the latest innovation in the entertainment industry.  He has helped to modernize software for industry leaders like Sony, Daimler, Intel, the Golden Globes, Siemens Wind Power, ABC, NBC, DC Comics, Warner Brothers & the Linux Foundation.

As the CEO and Founder of Metal Toad, an AWS Advanced Consulting Partner, his primary job is to "get the right people in the room".  This one responsibility is cross-functional and includes both external business development functions as well as internal delegation and leadership development.

A UCLA alumni, he also serves in the community as a Board Member for the Los Angeles Area Chamber of Commerce, the Beverly Hills Chamber of Commerce, and Stand for Children Oregon - a public education political advocacy group. As an outspoken advocate for entry-level job creation in tech he helped found the non-profit, P4TH, an organization dedicated to increasing the number of entry-level jobs in the tech industry, and is in the process of organizing an Advisory Board for the Bixel Exchange, a Los Angeles non-profit that provides almost 200 tech internships every year.


Have questions?