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.
分享到:
相关推荐
1. **打开Form1的设计器**:切换到Form1的设计视图。 2. **添加PrintControl和PrintBarManager**:从工具箱中拖拽PrintControl和PrintBarManager控件到Form1上。默认情况下,这些控件位于Visual Studio IDE的...
启示录 第二版 Marty Cagan(压缩卷第一部分,共三部分)
标题中的"Simple program demonstrating how to create a stop watch using the Timer control"揭示了这是一个关于使用Timer控件创建简单计时器程序的示例。这个程序可能是用Visual Basic (VB)编写的,因为文件名中...
How to Create a Database in Python using SQL Lite 3 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
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.
Charts I- How to create a chart.xlt
How to create Custom Views, Controls, and Layouts How to create 3D experience with OpenGL ES 2.0 How to achieve speed to market through JSON, Form processing, and Parse How to eliminate memory leaks ...
How to Create a Healthy Plate - English.srt
This is the source to my OCX. This demontrates how to create a basic OCX. This will aid anyone who wants to create an OCX.
This sample program demonstrates how to subclass a form using VB’s AddressOf operator.
This book is a practical guide on how to create artwork for computer games - a burgeoning area in which thousands of artists are hired each year. The guide mostly focuses on fantasy art but the ...
How to create a donut chart like this in chart.js
this book tell you How to Create a Simple Web Dynpro Application for java,not for ABAP
Charts I- How to create a chart(2).xlt
VCL Layout Control - How to Create and Customize a Simple Layout [720p]
This VB application demonstrates how to create an animation. Also contains a scroll bar which you can use to control the speed of the animation.