Drupal: How to Create Custom Contact Us Form
In this article I would like to share my experience in creating of custom Contact Us form. We will use standard Contact Drupal module as a basis for it.
So, you need to create custom Contact Us form, for example like this:
Let’s start!
First of all, enable Contact module. It is core Drupal module. It enables the use of personal and site-wide contact forms. The standard form created with Contact would contains the following fields: subject, name, email, message.
You can get know how to enable module here.
Then create custom module (for example, my_contact). You can get know more about creating custom modules from our articles or from Drupal documentation.
Place the following code into your new module:
function mycontact_theme() {
return array (‘mycontact’ => array (‘arguments’ => array (‘data’ => NULL ), ‘template’ => ‘tpl/mycontact’ ),
‘message’ => array (‘arguments’ => array (‘data’ => NULL ), ‘template’ => ‘tpl/message’ ) ); // Template file without extension.
}
function mycontact_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === ‘contact_mail_page’) {
// pr($form);
unset ( $form [‘subject’] );
$i == 0;
$form [‘contact_information’] = array (‘#value’ => theme ( ‘mycontact’ ), ‘#weight’ => $i );$i++;
$form [‘name’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘Name’ ), ‘#maxlength’ => 255, ‘#required’ => TRUE, ‘#weight’ => $i );$i++;
$form [‘company’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘Company’ ), ‘#maxlength’ => 255, ‘#weight’ => $i);$i++;
$form [‘phone’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘Phone’ ), ‘#maxlength’ => 255, ‘#weight’ => $i);$i++;
$form [‘mail’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘Email’ ), ‘#maxlength’ => 255, ‘#required’ => TRUE, ‘#weight’ => $i );$i++;
$form [‘sqft’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘How much Space are you looking for Sq.Ft?’ ), ‘#maxlength’ => 255, ‘#weight’ => $i );$i++;
$form [‘monthly-budget’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘What is your monthly budget ?’ ), ‘#maxlength’ => 255, ‘#weight’ => $i );$i++;
$form [‘business’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘What type of business are you in ?’ ), ‘#maxlength’ => 255, ‘#weight’ => $i );$i++;
$form[‘message’] = array(‘#type’ => ‘textarea’,’#title’ => t(‘How can we help you ?’),’#weight’ => $i);$i++;
if($form[‘copy’]){
$form[‘copy’][‘#weight’] = $i;$i++;
}
$form[‘submit’][‘#weight’] = $i;$i++;
$form[‘#submit’][0] = ‘mycontact_form_submit';
}
}
function mycontact_form_submit($form, &$form_state){
$form_state[‘values’][‘subject’] = ‘Metro Manhattan Office Space – Contact Us';
$form_state[‘values’][‘message’] = theme(‘message’, $form_state[‘values’]);
contact_mail_page_submit($form, $form_state);
}
Functions mycontact_form_alter, mycontact_form_submit, mycontact_theme are standard and you can read about them in Drupal documentation. Please search for:
- drupal hook_form_alter
- drupal hook_form_submit
- drupal hook_theme
mycontact_form_alter catch the form and make the following changes there:
- Removes Subject field:
unset ( $form [‘subject’] );
- Adds new fields to the form:
$form [‘contact_information’] = array (‘#value’ => theme ( ‘mycontact’ ), ‘#weight’ => $i );$i++;
$form [‘name’] = array (‘#type’ => ‘textfield’, ‘#title’ => t ( ‘Name’ ), ‘#maxlength’ => 255, ‘#required’ => TRUE, ‘#weight’ => $i );$i++;
etc.
mycontact_form_submit is my processor of the form. Once the user clicks Submit, form control is passed to my_contact module. My subject & message are added to the form. Then the form is passed to the standard control.
I also used two .tpl files for this form: one contains email template and the other – header for Contact Us form. Nevertheless, you can insert these directly to the module code.
Good luck!
There is a contributed module on drupal.org
http://drupal.org/project/contact_field which offers contact form customization.
Regards.
Thanks llot for this ‘How to’
It is clear and helpfull !!
Can you elaborate where to place the tpl files and how to configure
mycontact_theme function so this files will be called .
R.