Làm việc với Form API

Một phần của tài liệu Phát triển website với drupal 7 (Trang 34 - 40)

2 Xây dựng mô-đun lấy thông tin thời tiết tự động

2.4Làm việc với Form API

Drupal cung cấp một giao diện hỗ trợ lập trình (API) hỗ trợ việc tạo, kiểm tra và xử lý các Form html. Form API sẽ lưu trữ form dưới dạng các mảng lồng nhau gồm thuộc tính và giá trị. Mảng này sau đó được xem như là một phần trong tiến trình xử lý của Drupal và sẽ được hiển thị ra ở các trang có chứa form. Cách tiếp cận này sẽ có một số ưu điểm:

1. Thay vì tạo form bằng mã HTML, chúng ta chỉ cần tạo mảng và để cho Drupal enigne tạo form cho chúng ta.

2. Vì việc hiển thị form là một dạng dữ liệu có cấu trúc nên chúng ta dễ dàng thêm, sửa, xóa, sắp xếp lại hoặc thay đổi các thành phần của form. Điều này đặc biệt hữu dụng khi chúng ta muốn thay đổi các form được tạo bởi mô-đun khác.

3. Gán các thành phần của form vào các hàm theme. 4. Gán các thao tác xử lý, kiểm tra form.

5. Các thao tác với form được bảo vệ hoàn toàn trước các cuộc tấn công dạng injection.

PHÁT TRIỂN WEBSITE VỚI DRUPAL 7

ĐỒNG QUANG TRỌNG 47K - CNTT TRANG 35

Quá trình xử lý form

ĐỒNG QUANG TRỌNG 47K - CNTT TRANG 36

Tạo các Form cho mô-đun thời tiết

Tạo form hiển thị thông tin thời tiết

/*

* Create weather content */

function drupal_weather_form($form, &$form_state) {

$locations = array( 'VMXX0007' => t('TP HCM'), 'VMXX0006' => t('Ha Noi'), 'VMXX0005' => t('Hai Phong'), '7213884' => t('Buon Me Thuot'), 'VMXX0031' => t('Ca Mau'), '7213346' => t('Cam Pha'), 'VMXX0004' => t('Can Tho'), 'VMXX0020' => t('Cao Bang'), '8456' => t('Da Lat'), 'VMXX0028' => t('Da Nang'), '7208781' => t('Gia Lai'), '7208375' => t('Ha Giang'), '7208075' => t('Ha Tinh'), 'VMXX0009' => t('Hue'), 'VMXX0019' => t('Lao Cai'), '19163' => t('Long Xuyen'), '7201559' => t('Mong Cai'), 'VMXX0011' => t('Nam Dinh'), 'VMXX0029' => t('Nha Trang'), 'VMXX0012' => t('Phan Thiet'), '7196808' => t('Pleiku'), '27117' => t('Quy Nhon'), 'VMXX0015' => t('Thai Nguyen'), 'VMXX0026' => t('Vinh'), 'VMXX0018' => t('Vung Tau'), );

$variables = variable_get('drupal_weather');

$display_title = ($variables['display_title'] == 0) ? '' : t('Choose

location');

$form = array();

$form['drupal_weather_location'] = array(

'#type' => 'select',

'#title' => $display_title,

'#options' => $locations,

'#default_value' => $variables['display_default'],

'#ajax' => array(

// #ajax has two required keys: callback and wrapper.

// 'callback' is a function that will be called when this element

changes.

'callback' => 'drupal_weather_get_external_content',

// 'wrapper' is the HTML id of the page element that will be replaced.

'wrapper' => 'drupal-weather', (adsbygoogle = window.adsbygoogle || []).push({});

'effect' => $variables['effect_effect'],

PHÁT TRIỂN WEBSITE VỚI DRUPAL 7

ĐỒNG QUANG TRỌNG 47K - CNTT TRANG 37

'progress' => array('type' =>

$variables['effect_progress']),

), );

// This entire form element will be replaced whenever 'changethis' is updated.

$form['drupal_weather_div'] = array(

'#type' => 'markup',

'#prefix' => '<div id="drupal-weather">', '#suffix' => '</div>',

);

//try to submit this form at the firts time

if ($variables['display_auto'] && count($form_state)) {

$form['drupal_weather_div'] =

drupal_weather_get_external_content($form, $form_state); }

return $form; }

Tạo form cho người quản trị

/*

* Weather admin setting form */

function _binet_weather_admin() {

$variables = variable_get('binet_weather', array());

$locations = array( 'VMXX0007' => t('TP HCM'), 'VMXX0006' => t('Ha Noi'), 'VMXX0005' => t('Hai Phong'), '7213884' => t('Buon Me Thuot'), 'VMXX0031' => t('Ca Mau'), '7213346' => t('Cam Pha'), 'VMXX0004' => t('Can Tho'), 'VMXX0020' => t('Cao Bang'), '8456' => t('Da Lat'), 'VMXX0028' => t('Da Nang'), '7208781' => t('Gia Lai'), '7208375' => t('Ha Giang'), '7208075' => t('Ha Tinh'), 'VMXX0009' => t('Hue'), 'VMXX0019' => t('Lao Cai'), '19163' => t('Long Xuyen'), '7201559' => t('Mong Cai'), 'VMXX0011' => t('Nam Dinh'), 'VMXX0029' => t('Nha Trang'), 'VMXX0012' => t('Phan Thiet'), '7196808' => t('Pleiku'), '27117' => t('Quy Nhon'), 'VMXX0015' => t('Thai Nguyen'), 'VMXX0026' => t('Vinh'), 'VMXX0018' => t('Vung Tau'), ); $form = array();

ĐỒNG QUANG TRỌNG 47K - CNTT TRANG 38

'#title' => 'Display settings', );

$form['binet_weather_display']['display_title'] = array(

'#type' => 'checkbox',

'#title' => 'Title',

'#description' => 'Display or hide title',

'#default_value' => empty($variables['display_title']) ? 0 :

$variables['display_title'], (adsbygoogle = window.adsbygoogle || []).push({});

);

$form['binet_weather_display']['display_auto'] = array(

'#type' => 'checkbox',

'#title' => 'Display result at first time',

'#default_value' => empty($variables['display_auto']) ? 0 :

$variables['display_auto'],

);

$form['binet_weather_display']['display_default'] = array(

'#type' => 'select',

'#title' => 'Default location',

'#options' => $locations,

'#default_value' => empty($variables['display_default']) ?

'VMXX0026' : $variables['display_default'], );

$form['binet_weather_effect'] = array(

'#type' => 'fieldset',

'#title' => 'Effect settings', );

$form['binet_weather_effect']['effect_effect'] = array(

'#type' => 'textfield',

'#title' => 'Effect',

'#description' => 'Choose style for change effect. Default value is <i>fade</i>.',

'#default_value' => empty($variables['effect_effect']) ? 'fade' :

$variables['effect_effect'],

);

$form['binet_weather_effect']['effect_speed'] = array(

'#type' => 'textfield',

'#title' => 'Speed',

'#description' => 'Speed of effect. Default value is <i>slow</i>',

'#default_value' => empty($variables['effect_speed']) ? 'slow' :

$variables['effect_speed'],

); (adsbygoogle = window.adsbygoogle || []).push({});

$form['binet_weather_effect']['effect_progress'] = array(

'#type' => 'textfield',

'#title' => 'Progress',

'#description' => 'type of progress. Default value is <i>throbber</i>.',

'#default_value' => empty($variables['effect_progress']) ?

'throbber' : $variables['effect_progress'], );

$form['binet_weather_pattern'] = array(

'#type' => 'fieldset',

'#title' => 'Replacement Pattern', );

$form['binet_weather_pattern']['pattern_stylesheet_key'] = array (

PHÁT TRIỂN WEBSITE VỚI DRUPAL 7

ĐỒNG QUANG TRỌNG 47K - CNTT TRANG 39

'#title' => 'Stylesheet Pattern',

'#default_value' => empty($variables['pattern_stylesheet_key']) ?

'/<link\s+rel="stylesheet.*?>/si' : $variables['pattern_stylesheet_key'],

'#prefix' => '<table><tr><td>',

'#suffix' => '</td>', );

$form['binet_weather_pattern']['pattern_stylesheet_value'] = array (

'#type' => 'textfield',

'#title' => 'Stylesheet Replacement',

'#default_value' => empty($variables['pattern_stylesheet_value'])

? '' : $variables['pattern_stylesheet_value'],

'#prefix' => '<td>',

'#suffix' => '</td></tr>', );

$form['binet_weather_pattern']['pattern_image_key'] = array (

'#type' => 'textfield',

'#title' => 'Image Pattern',

'#default_value' => empty($variables['pattern_image_key']) ?

'/<div\s+style="width: 110px; float: left; text-align: center;.*?>/' :

$variables['pattern_image_key'], (adsbygoogle = window.adsbygoogle || []).push({});

'#prefix' => '<tr><td>',

'#suffix' => '</td>', );

$form['binet_weather_pattern']['pattern_image_value'] = array (

'#type' => 'textfield',

'#title' => 'Image Replacement',

'#default_value' => empty($variables['pattern_image_value']) ?

'<div class="image">' : $variables['pattern_image_value'],

'#prefix' => '<td>',

'#suffix' => '</td></tr>', );

$form['binet_weather_pattern']['pattern_text_key'] = array (

'#type' => 'textfield',

'#title' => 'Text Pattern',

'#default_value' => empty($variables['pattern_text_key']) ?

'/<div\s+style="width: 165px; float: left; text-align: left; margin-top: 5px;.*?>/' : $variables['pattern_text_key'],

'#prefix' => '<tr><td>',

'#suffix' => '</td>', );

$form['binet_weather_pattern']['pattern_text_value'] = array (

'#type' => 'textfield',

'#title' => 'Text Replacement',

'#default_value' => empty($variables['pattern_text_value']) ?

'<div class="text">' : $variables['pattern_text_value'],

'#prefix' => '<td>',

'#suffix' => '</td></tr>', );

$form['binet_weather_pattern']['pattern_font_key'] = array (

'#type' => 'textfield',

'#title' => 'Font Pattern',

'#default_value' => empty($variables['pattern_font_key']) ?

'/<span\s+class="fontsize12.*?>/' : $variables['pattern_font_key'],

'#prefix' => '<tr><td>',

'#suffix' => '</td>', ); (adsbygoogle = window.adsbygoogle || []).push({});

ĐỒNG QUANG TRỌNG 47K - CNTT TRANG 40

'#title' => 'Font Replacement',

'#default_value' => empty($variables['pattern_font_value']) ?

'<span>' : $variables['pattern_font_value'],

'#prefix' => '<td>',

'#suffix' => '</td></tr></table>', );

$form['submit'] = array(

'#type' => 'submit',

'#value' => 'Submit', );

$form['default'] = array(

'#type' => 'submit',

'#value' => 'Default',

'#submit' => array('_binet_weather_admin_default'), );

return $form; }

/*

* Submit settings form */

function _binet_weather_admin_submit($form, &$form_state) {

$variables = $form_state['values'];

variable_set('binet_weather', $variables);

drupal_set_message(t('The configuration options have been saved.'));

}

function _binet_weather_admin_default($form, &$form_state) {

variable_del('binet_weather');

drupal_set_message(t('Default values has been set. Please save form to

apply new values.')); }

Sau khi xây dựng xong các form sử dụng Form API, để gọi các form, ta chỉ cần sử dụng hàm drupal_get_form().

Một phần của tài liệu Phát triển website với drupal 7 (Trang 34 - 40)