ไฟล์ .module
ไฟล์ .module (การ comment ในไฟล์นี้ใช้เครื่องหมาย // หรือ /*..*/) ใช้หลักการเขียนโปรแกรมแบบ PHP นะจ๊ะ
- ฟังก์ชั่นของโมดูลที่ใช้ใน drupal มีชื่อว่า {module-name}_{hook}
- ชนิดต่างๆ ของ hook ดูที่นี่ได้นะ --> http://api.drupal.org/api/group/hooks/5
- hook ที่ต้องมีในทุกโมดูล คือ
hook_node_info()
hook_perm()
hook_access()
hook_form() - hook ที่เกี่ยวกับการใช้งานฟิลด์ ได้แก่
hook_insert()
hook_update()
hook_delete()
hook_validate()
hook_nodeapi()
hook_view()
hook_load() - hook ที่น่าสนใจคือ hook_help()
- ก่อนการ hook ทุกครั้งควรมี comment ดังนี้ (การ comment จะเป็นการช่วยสร้าง help)
/**
* Implementation of hook_{hook name here}().
*/
- เริ่มต้นบรรทัดแรกของโมดูลด้วย <?php
- hook_node_info() รายละเอียดทั่วไปของโมดูล array ที่แสดงอยู่ข้างล่างนี้เป็นค่าที่จำเป็นต้อง define ให้ นอกจากนี้ยังสามารถ define ค่าอื่นๆได้อีก
Attribute ที่สามารถใช้ได้มีมากกว่า 10 ชนิด
"name": ชื่อชนิดของโหนด (จำเป็นต้องมี)
"module": โมดูลที่ต้องนำมาใช้ร่วมกัน
"description": รายละเอียดของโหนด(จำเป็นต้องมี)
"help": text that will be displayed at the top of the submission form for
this content type. Optional (defaults to '').
"has_title": boolean indicating whether or not this node type has a title
field. Optional (defaults to TRUE).
"title_label": the label for the title field of this content type.
Optional (defaults to 'Title').
"has_body": boolean indicating whether or not this node type has a body
field. Optional (defaults to TRUE).
"body_label": the label for the body field of this content type. Optional
(defaults to 'Body').
"min_word_count": the minimum number of words for the body field to be
considered valid for this content type. Optional (defaults to 0).
"locked": boolean indicating whether the machine-readable name of this
content type can (FALSE) or cannot (TRUE) be edited by a site
administrator. Optional (defaults to TRUE).
<?php
function test_node_info() {
return array (
'test' => array(
'name' => t('test node'),
'module' => 'test',
'description' => t('This is test module.')
),
);
}
?>
- hook_perm() เป็นการกำหนด permission ให้ user สามารถสร้างหรือแก้ไข node ที่สร้างขึ้นใหม่ได้
มีรูปแบบคือ verb+ modulename
<?php
function test_perm() {
return array ('create test', 'edit own test');
}
?>
- hook_access() สำหรับ user ที่ได้รับสิทธิ์ในการเข้าถึงโหนดสามารถเข้าถึงโหนดด้วยการสร้าง หรืออัพเดต และลบ โหนดที่สร้างนั้นๆ
<?php
function test_access($op, $node) { //op = operating การกระทำกับ page,node,teaser,etc.
global $user; //global คือ user ทั้งหมดที่เป็นสมาชิกใน drupal
if($op == 'create') { //user ที่ได้รับอนุญาตเท่านั้นที่สามารถสร้างโหนดได้
return user_access('create test');
}
if ($op == 'update' || $op == 'delete') { //user ที่ได้สร้างโหนดเท่านั้น สามารถเปลี่ยนแปลงหรือลบโหนดได้
if(user_access('edit own test') && ($user -> uid == $node -> uid)) {
return TRUE;
}
}
}
?> -
hook_form() ใช้เพื่อแสดงฟอร์มของโหนด ซึ่ง hook นี้จะต้องประกอบด้วย array ที่มีarray ย่อยเพื่อระบุข้อมูลแต่ละส่วนของฟอร์ม
<?php
function test_form(&$node) {
$type = node_get_types ('type', $node);
/*define ค่าของฟอร์มในส่วนของ title และ body
ชนิดต่างๆ ของฟอร์ม และค่าต่างๆ ที่สามารถ define ได้
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/5
*/
$form['title'] = array(
'#type' =>'textfield',
'#title' => check_plain($type -> title_label),
'#required' => TRUE,
'#default_value' => $node -> title,
'#weight' => -5
);
//ต้องการส่วนของ body และ filter
$form['body_filter']['body'] = array(
'#type' =>'textarea',
'#title' => check_plain($type -> body_label),
'#default_value' => $node -> body,
'#required' => FALSE
);
$form['body_filter']['filter'] = filter_form($node -> format);return $form;
}
?> -
hook_help()
<?php
function test_help($section) {
switch ($section) {
case 'admin/help#test':
return t('This is help for test module...........');
break;
}
}
?> -
ตัวอย่าง
<?php
// $Id$
/**
* @file
* Lets users add private annotations to nodes.
*
* Adds a text field when a node is displayed
* so that authenticated users may make notes.
*/
/**
* Implementation of hook_node_info().
*/
function test_node_info() {
return array (
'test' => array(
'name' => t('test node'),
'module' => 'test',
'description' => t('This is test module.')
),
);
}
/**
* Implementation of hook_perm().
*/
function test_perm() {
return array ('create test', 'edit own test');
}
/**
* Implementation of hook_access().
*/
function test_access($op, $node) {
global $user;
if($op == 'create') {
return user_access('create test');
}
if ($op == 'update' || $op == 'delete') {
if(user_access('edit own test') && ($user -> uid == $node -> uid)) {
return TRUE;
}
}
}
/**
* Implementation of hook_form().
*/
function test_form(&$node) {
$type = node_get_types ('type', $node);
$form['title'] = array(
'#type' =>'textfield',
'#title' => check_plain($type -> title_label),
'#required' => TRUE,
'#default_value' => $node -> title,
'#weight' => -5
);
$form['body_filter']['body'] = array(
'#type' =>'textarea',
'#title' => check_plain($type -> body_label),
'#default_value' => $node -> body,
'#required' => FALSE
);
$form['body_filter']['filter'] = filter_form($node -> format);
return $form;
}
/**
* Implementation of hook_help().
*/
function test_help($section) {
switch ($section) {
case 'admin/help#test':
return t('This is help for test module...........');
break;
}
}
?>
รอก่อนนะจะมาเพิ่มอีก