Drupal: .htaccess Access Deny
August 21, 2014 – 7:59 am | No Comment

In this article I will tell how to forbid access to certain resources for some clients. The instructions will include descriptions of different directives.

Read the full story »
CSS Templates

Contain reviews and news about CSS Templates.

Freebies

Contain freebies such as icons, graphics, headers and images for your websites.

Fun Stuff

Contains other fun stuff for entertainment or interesting site showcase.

How-To

Contain technical elaborations on some specific workarounds or common tweak.

Joomla Templates

Contains reviews and news about Joomla templates.

Home » Fun Stuff

Drupal: How to Create Custom Contact Us Form

Submitted by on March 26, 2010 – 10:40 am2 Comments

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:

clip_image002

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!

2 Comments »

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.