Lập trình drupal - Viết một module mô tả ngắn cho nodes và quản trị nodes đó.

longt8x

New member
Bạn mở trình soạn thảo văn bản vd Adobe dreamwave hoặc zendtool, phpeditor,net bean... vào site/all/modules/custom
Tạo mới một thư mục tên summary | site/all/modules/custom/summary
Tạo file thông tin modules .info : summary.info | site/all/modules/custom/summary/summary.info
Code:

name = Summary
description = "Cho phep nguoi dung mo ta ngan cho nodes"
package = Pro Drupal Development
core = 7.x
files[] = summary.module
files[] = summary.install
files[] = summary.admin.inc
configure=admin/config/summary/settings

Tiếp đó bạn xây dựng summary.module trong /sites/all/modules/custom/summary/summary.module
Mã:
<?php
/**
* @file
*/
/**
* Implementation of hook_menu().
* Ham hook_menu dung de tao cac duong link va callback den noi dung cua duong link do
*/
function summary_menu() {
$items['admin/config/summary'] = array(
'title' => 'Mo ta van tat',
'description' => 'Dieu chinh thong tin cua mo ta van tat.',
'position' => 'right',
'weight' => -5,
'page callback' => 'system_admin_menu_block_page', //goi den core drupal
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/summary/settings'] = array(
'title' => 'Thiet lap cau hinh cho summary',
'description' => 'Thay doi cau hinh summary.',
'page callback' => 'drupal_get_form',
'page arguments' => array('summary_admin_settings'), //goi den ham tu xay dung.
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'summary.admin.inc',
);
return $items;
}

Tại đây chúng ta tạo 2 đường link là site_name/admin/config/summary và sitename/admin/config/summary/settings trong đó đường link đầu tiên gọi trực tiếp đến core drupal để thực hiện, và đường link thứ 2 gọi đến hàm custom chúng ta tự xây dựng, page arguments là tham số để hàm truyền đến nội dung tương ứng , tại đây hàm summary_admin_settings được gọi và chúng ta ko viết trực tiếp trong file modules này mà chúng ta gọi riêng nó qua file summary.admin.inc

Bạn tạo file summary.admin.inc ngang hàng với các file .info, .module

Code:

<?php
/**
* @file
* Trang quan tri admin duoc goi tu summary.module
*/
/**
* Xay dung form. cau hinh lai. summary
*
*/
function summary_admin_settings() {

$types = node_type_get_types();//function from drupal_API
foreach($types as $node_type) {
$options[$node_type->type] = $node_type->name;
}
//tao mot check box
$form['summary_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('User co the mo ta van tat content types'),
'#options' => $options,
'#default_value' => variable_get('summary_node_types', array('page')),
'#description' => t('A text field will be available on these content types to
make user-specific notes.'),
);
$form['#submit'][] = 'summary_admin_settings_submit';
return system_settings_form($form);
}

Sau khi thực hiện xong bạn vào trình duyệt gõ đường link thứ 2 sitename/admin/config/summary/settings để xem thành quả.

Tại đây bạn đã có thể nhìn thấy các thông tin quản trị và check box để kích hoạt, tuy nhiên khi submit bạn ko thấy drupal có biểu hiện gì...Đó là điều chúng ta phải thực hiện bây giờ.
Drupal cung cấp hàm thực hiện sau khi submit hook_form_submit().
Ta viết tiếp vào file summary.admin.inc
Code:

function summary_admin_settings_submit($form, $form_state) {

foreach ($form_state['values']['annotate_node_types'] as $key => $value) {
//gia tri cua form ma chung ta vua tao.
if (!$value) {
//tao them mot field du lieu vao database co ten summary
$instance = field_info_instance('node', 'summary', $key);
if (!empty($instance)) {
//neu ton tai thi delete field cũ đi
field_delete_instance($instance);
watchdog("Summary", 'Delete summary từ content type:
%key', array('%key' => $key));
}
} else {
//nếu không tìm thấy field nào trùng, chúng tao tạo mới field
$instance = field_info_instance('node', 'summary', $key);
if (empty($instance)) {
$instance = array(
'field_name' => 'summary',
'entity_type' => 'node',
'bundle' => $key,
'label' => t('Mo ta ngan'),
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'type' => 'text_default',
),
'teaser' => array(
'type' => 'text_summary_or_trimmed',
),
),
);
$instance = field_create_instance($instance);
watchdog('Summary', 'Thêm mới field Summary từ content type: %key',
array('%key' => $key));
}
}
} // End foreach loop.
}

Chúng ta quay lại trình duyệt và click vào nút button chúng ta sẽ tạo được một filed mới có tên summary, bạn có thể vào cơ sở dữ liệu phpmyadmin để xem kết quả.

Tiếp theo chúng ta xây dựng file summary.install, chức năng của file này trong drupal chỉ có tác dụng, thêm mới một thực thể(entity), xóa bỏ một thực thể hoặc chuyển đổi nó.
Drupal cung cấp hook_install() để tạo mới một biểu đồ [schema]

Code:

<?php
/**
* Implements hook_install()
*/
function summary_install() {

$field = field_info_field('summary');

if (empty($field)) {
//kiem tra neu bang do ko ton tai thi tao bang, tuy nhien chung ta da tao roi =.=
$field = array(
'field_name' => 'summary',
'type' => 'text_with_summary',
'entity_types' => array('node'),
'translatable' => TRUE,
);
$field = field_create_field($field);
}
}

Tiếp theo cài đặt một schema thì các bạn cũng sẽ được cung cấp hàm gỡ bỏ bảng đó ra khỏi CSDL , drupal cung cap hook_uninstall().
Code:

<?ph
/**
* Implements hook_uninstall()
*/
function annotate_uninstall() {
watchdog("Summary Module", "Uninstalling module and deleting fields");
$types = node_type_get_types();
foreach($types as $type) {
//tao ham xoa bo module summary
summary_delete_summary($type);

}
$field = field_info_field('summary');
if ($field) {
field_delete_field('summary');
}
}
/**--Ham xoa bo summary---*/
function summary_delete_summary($type) {
$instance = field_info_instance('node', 'summary', $type->type);
if ($instance) {
field_delete_instance($instance);
}
}

Tiếp theo ta quay lai file .module và kiểm tra nếu không phải user tạo ra node đó thì node đó sẽ bị ẩn đi, drupal cung cấp hook_node_load() để tạo ra khởi tạo ban đầu trước khi node được thực hiện.
Code:

/**
* Implements hook_node_load()
*/
function summary_node_load($nodes, $types) {
//goi den user
global $user;

foreach ($nodes as $node) {
if ($user->uid != $node->uid) {
unset($node->summary);
}
}
}

Sau khi thực hiện xong bạn có thể tạo một node bất kì để xem thành quả, ví dụ...bạn Add content types -> chọn Articles, trong đó bạn sẽ nhìn thấy trường mô tả ngắn do bạn mới thiết lập.

Nếu bạn muốn phần quản trị của module này dễ tìm thấy trong config ta thay thế phần định nghĩa link [admin/config/summary] trong file module bằng đoạn sau :
Code:

/**
* Implementation of hook_menu().
*/
function summary_menu() {
$items['admin/config/summary'] = array(
'title' => 'Mo ta ngan',
'description' => 'Dieu chinh thong tin mota ngan.',
'position' => 'right',
'weight' => -5,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/summary/settings'] = array(
'title' => 'Thiet lap mo ta ngan',
'description' => 'Thay doi noi dung mo ta ngan.',
'page callback' => 'drupal_get_form',
'page arguments' => array('summary_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'summary.admin.inc',
);
return $items;
}

Nguồn http://seodrupal.vn
 
Back
Top