Module mau: MoreFieldContactPage

Module mau hom nay gioi thieu cach su dung hook_form_alter() va khoa #theme va #submit trong FormAPI.

Muc dich cua module:
- (duong nhien la lam module mau)
- Tao them mot field "Company" cho trang lien he (duoc cung cap boi module contact)

1. Cau truc to thuc tap tin

  1. ./sites/all/modules/MoreFieldContactPage
  2. ./sites/all/modules/MoreFieldContactPage/MoreFieldContactPage.info
  3. ./sites/all/modules/MoreFieldContactPage/MoreFieldContactPage.module

2. Dinh nghia tap tin MoreFieldContactPage.info

Tap tin nay se dinh nghia:
- Ten trinh bay cua module: MoreFieldContactPage
- Noi dung mo ta: Add more fields to core contact page
- Module phu thuoc: contact
- Phien ban: 5.x-1.0-dev
- Nam trong goi: ToiLa .net (giup cho nguoi dieu hanh de quan ly)

Noi dung cu the

  1. name = MoreFieldContactPage
  2. description = Add more fields to core contact page
  3. dependencies = contact
  4. version = 5.x-1.0-dev
  5. package = ToiLa .net

3. Su dung hook_form_alter()

Truoc khi mot HTML Form duoc tao ra, Drupal se di tim tat ca cac ham co dang {ten_module}_form_alter() de thay noi cau truc mang dinh nghia form. O module nay cua chung ta, chung ta se dinh nghia ham (trong tap tin MoreFieldContactPage:

<?php
function MoreFieldContactPage_form_alter($form_id, &$form) {
  print(
'['.$form_id.']');
}
?>

Khi bat cu mot HTML Form nao tao ra, ham cua MoreFieldContactPage_form_alter() cua chung ta deu duoc goi. Khi duoc goi, ham se in ra man hinh ma so cua form ($form_id)tuong ung. Di vao trang ?q=contact, chung ta se biet duoc ma so cua form lien he se la contact_mail_page. Dua vao $form_id nay, chung ta tiep tuc cai tien ham MoreFieldContactPage_form_alter() de khi, moi khi form lien he duoc tao ra, chung ta se them vao mot field (truong) co khoa la company, co tieu de la Your compapy name, co phan mo ta la This field is generated using MoreFieldContactedPage module. va noi dung khong duoc rong:

<?php
function MoreFieldContactPage_form_alter($form_id, &$form) {
  if (
$form_id == 'contact_mail_page') {
    
$form['company'] = array(
      
'#type' => 'textfield',
      
'#title' => 'Your company name',
      
'#description' => 'This field is generated using MoreFieldContactedPage module.',
      
'#required' => true
    
);
  }
}
?>

3. Su dung khoa #theme cua FormAPI

Chung ta co the su dung khoa #theme cua FormAPI de co the thay doi cach trinh bay cua HTML Form duoc xuat ra. Chung ta se tiep tuc cai tien ham MoreFieldContactPage_form_alter(), de truoc khi form lien he duoc xuat ra, no se duoc ham theme_ContactForm thay doi cach trinh bay ra giao dien nguoi dung:

<?php
function MoreFieldContactPage_form_alter($form_id, &$form) {
  if (
$form_id == 'contact_mail_page') {
    
$form['#theme'] = 'ContactForm';
    
$form['company'] = array(
      
'#type' => 'textfield',
      
'#title' => 'Your company name',
      
'#description' => 'This field is generated using MoreFieldContactedPage module.',
      
'#required' => true
    
);
  }
}
?>

Va duong nhien, chung ta se phai dinh nghia ham theme_ContactForm():

<?php
function theme_ContactForm($form) {
  
$output '';
  
$output .= drupal_render($form['contact_information']);
  
$output .= drupal_render($form['company']);
  
$output .= drupal_render($form);
  return 
$output;
}
?>

Y nghia cua ham,
- Dong 2: chung ta su dung bien $output de luu giu chuoi HTML cua Form lien he.
- Dong 3: Bat dau form lien he se la thong tin cua form
- Dong 4: Tiep den la field 'Your company'
- Dong 5: Tiep sau la cac thanh phan con lai cua form
- Dong 6: Tra ve chuoi HTML cua Form lien he.

4. Su dung khoa #submit cua FormAPI

Khi da them (cac) field can thiet vao form lien he, chung ta can them phai thay doi mot chut cach xu ly khi form duoc de trinh (nguoi dung nhap vao nut Submit).
3.1. Khai bao ham se xu ly form
Neu ham xu ly form khong duoc khai bao, mac dinh, FormAPI se hieu ham {form_id}_submit() se la ham xu ly khi form duoc de trinh. Chung ta mot lan nua cai tien ham MoreFieldContactPage_form_alter(), dinh nghia ham xu ly form se la MoreFieldContactPage_submit():

<?php
function MoreFieldContactPage_form_alter($form_id, &$form) {
  if (
$form_id == 'contact_mail_page') {
    
$form['#theme'] = 'ContactForm';
    
$form['company'] = array(
      
'#type' => 'textfield',
      
'#title' => 'Your company name',
      
'#description' => 'This field is generated using MoreFieldContactedPage module.',
      
'#required' => true
    
);
    
$form['#submit'] = array('MoreFieldContactPage_submit' => array());
  }
}
?>

Va ham MoreFieldContactPage_submit() cua chung ta se duoc dinh nghia dai khai nhu sau (chen them ten cong ty cua nguoi dung vao noi dung chinh cua thong diep):

<?php
function MoreFieldContactPage_submit ($form_id$form_values) {
  
$form_values['message'] = "Contact's company: ".$form['company']."\r\n\r\n"
  
contact_mail_page_submit($form_id$form_values);
}
?>

5. Tham khao

- Module contact (trong ban phan phoi chinh cua Drupal)
- Form API

The Hong