Sức mạnh form_alter
hook_form_alter là một công cụ cực kỳ mạnh mà Drupal cung cấp. Chúng ta có thể sử dụng nó để thực hiện được nhiều việc, tưởng chừng như không thể: Thay đổi cấu trúc một form mà không cần sửa mã nguồn, định dạng form, thêm kịch bản xử lý cá nhân vào form hệ thống, ...
1. Trước hết chúng ta hãy thử tạo một form đơn giản theo những bước sau:
i. Tạo cấu trúc tập tin cho module có tên 'demo'
sites/all/modules/demo sites/all/modules/demo/demo.info sites/all/modules/demo/demo.module
ii. Mô tả module 'demo' -- định nghĩa tập tin demo.info
name = demo version = 5.x-dev package = @toila.net description = Demo module, created by The Hong
iii. Khai báo kịch bản của module 'demo' -- định nghĩa tập tin demo.module
<?php
/**
* Implemetation of hook_menu
*/
function demo_menu ($may_cache) {
if ($may_cache) {
return array (
array (
'path' => 'demo',
'access' => true,
'title' => 'Thehong\'s testing form',
'callback' => 'drupal_get_form',
'callback arguments' => array ('demo_demo')
)
);
}
}
/**
* Define array structure for our testing <a href="/taxonomy/term/229" title="">form</a>
*/
function demo_demo () {
return array (
'mytextfield' => array (
'#type' => 'textfield',
'#title' => 'My textfield',
'#default_value' => 'http://toila.net/',
'#description' => 'This is my text field!'
)
);
}
/**
* Handler for demo_demo form
*/
function demo_demo_submit ($form_id, $form_values) {
drupal_set_message ('Called demo_demo_submit ()');
}
?>iv. Khi đã kích hoạt module 'demo', chúng ta sẽ thấy form sau khi truy cập trang ?q=demo

2. Giới thiệu hook_form_alter
Như chúng ta đã thấy, tất cả form được tạo ra trong Drupal đều được định nghĩa từ những mảng dữ liệu. Trước khi các form được xuất ra giao diện người dùng, Drupal chuyển cấu trúc mảng này qua các hook_form_alter, cho phép các hook này thay đổi hoàn toàn nội dung mảng được truyền vào.
Dựa trên nguyên tắc này, nhóm phát triển Drupal đã xây dựng rất thành công các kiến trúc mạnh khác: nodeAPI, CCK module, i18n, ... Chúng ta sẽ dựa trên form demo_demo được định nghĩa trong module 'demo' ở trên để có cái nhìn chung về sức mạnh của FormAPI nói riêng và Drupal framework nói chung.
3. Thêm field vào form
Ở thí dụ này, chúng ta sẽ thêm một button submit vào form demo_demo, mà không cần phải sửa hàm demo_demo (). Mở file demo.module và thêm vào hàm sau:
<?php
/**
* Implementation of hook_form_alter
*/
function demo_form_alter ($form_id, &$form) {
// because hook_form_alter have effected on every forms created by system
// so, we only want edit form demo_demo
if ($form_id != 'demo_demo') {
return;
}
$form['submit'] = array (
'#type' => 'submit',
'#value' => 'Do submit'
);
}
?>Truy cập lại trang ?q=demo, chúng ta thấy form chúng ta đã thay đổi.

4. Xóa field khỏi form
Trong hàm vừa được định nghĩa ở trên, nếu chúng ta thêm dòng lệnh unset ($form['mytextfield']);:
<?php
/**
* Implementation of hook_form_alter
*/
function demo_form_alter ($form_id, &$form) {
// because hook_form_alter have effected on every forms created by system
// so, we only want edit form demo_demo
if ($form_id != 'demo_demo') {
return;
}
$form['submit'] = array (
'#type' => 'submit',
'#value' => 'Do submit'
);
unset ($form['mytextfield']);
}
?>Text field sẽ được di chuyển khỏi form demo_demo.

5. Định dạng lại form
Bằng một sổ thủ thuật, chúng ta có thể sử dụng hook_form_alter để định dạng lại form. Thí dụ sau:
- Thay đổi thứ tự trình bày text field và submit field
- Canh phải text field
<?php
/**
* Implementation of hook_form_alter
*/
function demo_form_alter ($form_id, &$form) {
// because hook_form_alter have effected on every forms created by system
// so, we only want edit form demo_demo
if ($form_id != 'demo_demo') {
return;
}
$form['submit'] = array (
'#type' => 'submit',
'#value' => 'Do submit',
'#weight' => -1
);
$form['mytextfield']['#field_prefix'] = '<div style="text-align: right">';
$form['mytextfield']['#field_suffix'] = '</div>';
}
?>
6. Thêm kịch bản xử lý cá nhân
Theo luồng xử lý một form, khi một form được đệ trình, thỏa mãn được các ràng buộc, sẽ được hàm {form_id}_submit xử lý. Sử dụng hook_form_alter, chúng ta có thể thêm vào kịch bản xử lý cá nhân. Chúng ta cùng xem xét thí dụ sau:
<?php
/**
* Implementation of hook_form_alter
*/
function demo_form_alter ($form_id, &$form) {
// because hook_form_alter have effected on every forms created by system
// so, we only want edit form demo_demo
if ($form_id != 'demo_demo') {
return;
}
$form['submit'] = array (
'#type' => 'submit',
'#value' => 'Do submit',
);
$form['#submit']['demo_custom_submit'] = array ();
}
/**
* Our customize submit handler
*/
function demo_custom_submit ($form_id, $form_values) {
drupal_set_message ('Called demo_custom_submit ()');
}
?>Khi form được đệ trình, chúng ta có 2 kịch bản xử lý form được thực thi.

Thế Hồng
Unsigned version
1 week 3 hours ago
2 weeks 5 days ago
7 weeks 3 days ago
7 weeks 3 days ago
7 weeks 3 days ago
9 weeks 3 days ago
10 weeks 3 days ago
11 weeks 3 days ago
12 weeks 9 hours ago
12 weeks 18 hours ago