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:
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.
分享到:
相关推荐
How to create a custom preview form
Simple program demonstrating how to create a stop watch using the Timer control.
This demonstrates how to create a splash form for your program.
Smashing eBook #5: How to Create Selling E-Commerce Websites 3 Preface Designing and developing for e-commerce takes a lot of brain work, knowledge and research before you start. You don‘t only have...
启示录 第二版 Marty Cagan(压缩卷第一部分,共三部分)
How To Install Custom Themes
How to create Web sites and applications with HTML, CSS ( PDFDrive.com ) .pdf
This application shows how to create a form with a fade out background.
How to Create a Database in Python using SQL Lite 3 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
How to Draw Custom Text on a Delphi Form's Caption Bar.pdf
how to create my appidshow to create my appidshow to create my appids
启示录 第二版 Marty Cagan (压缩卷第二部分,共三部分) INSPIRED_ How to Create Tech Products Customers Love, 2nd Edition part2.rar
You will learn about the basic plots, how to customize them, and combine them to make sophisticated figures. Along with basic plots, you will also learn to make professional scientific plots.
How to Create an eCATT
How to create a shortcut to another file
How to Create your Own Rule .pdf
HOW TO create the WebGoat workspace
How To Automate Excel to Create And Format a New Workbook Using C++ .NET and MFC
How to create your library of win 32 static c library
How to Use Custom Automation Algorithms for Data Labeling.zip