How to Add a Field to the Drupal Contact form

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 metaltoad.com:

<?php	
 
/**
 * Implementation of hook_form_alter().
 * @see http://api.drupal.org/api/function/hook_form_alter
 * 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.

Filed under: 


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.

Thanks


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


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!


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

thanks


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:

hook_form_FORM_ID_alter()

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'] . '"';

. . .
}
}

About the Author

Joaquin Lippincott, President &amp; Founder

Joaquin is a modern technology veteran. Throughout his career he has built successful digital strategies with a wide assortment of transformative technologies and platforms, helping clients identify a clear path for success.

Over the years he has worked with industry leaders such as DC Comics, the Emmys, Intel, Technicolor, Verizon Wireless, Habitat for Humanity, Limewire, the Linux Foundation, Sony Pictures Television, Mercy Corps, and Cisco as well as numerous small businesses, advertising agencies and internet start ups. With over 14 years of experience in his field he is still as passionate about technology as ever.

Joaquin is a graduate from UCLA with a degree in design and has also served on the AIGA Portland chapter board as President.

Twitter: @joaquinlippinco

Interested? Let's talk.