Tuesday, March 9, 2010

How to Add a Field to Drupal Contact form

STEP 1 :

First you have to create a module. Create a folder into /sites/all/modules. Give the folder name your module name. Create a module file into the folder. Give the module a name. I have used “contactus.module”. In contactus.module file write the following lines. If you give different module name then the function name should be function yourmodulename_form_alter(&$form, $form_state).


#title    = Title of the input field.

#type     = Type of the input field (e.g. ‘textfield’ or ‘textarea’ etc.  )

#required = If the field is required then value will be true.

Unset function will disable the particular field. Here, in the example I unset copy field. You can re-order all the fields as per need. Just you have to the $ order array. For more information see http://api.drupal.org/api/function/hook_form_alter.

[sourcecode language="php"]

<?php

function contactus_form_alter(&$form, $form_state) {

$form['company'] = array(

'#title' => t('Your company'),

'#type' => 'textfield',

);

$form['phone'] = array(

'#title' => t('Your phone'),

'#type' => 'textfield',

'#required' =>'true',

);

unset($form['copy']);

// reorder the elements in the form to include the elements being inserted

$order = array('name','mail', 'subject', 'company', 'phone','message','submit');

foreach($order as $key => $field) {

$form[$field]['#weight'] = $key;

}

[/sourcecode]

STEP  2: Now you have to create a info file.File name should be your modulename.info . Here I’ve used contactus.info. Into the .info file write the following lines.

name = "contactus"


description = "contact with us"


dependencies[] = contact


core = 6.x


php = 5.1

3 comments:

Frokostordning said...

Hmm that's amazing but to be honest i have a hard time determining it... I'm wondering what others have to say....

Queerajeorere said...

i easily enjoy your writing style, very charming.
don't give up and keep posting for the reason that it simply just very well worth to read it,
impatient to look at more of your well written articles, cheers ;)

konkurrencer said...

Thought I would spend time to read and comment on most of your articles today as the weather is so shocking :(