• Drupal CT,,2 *~
••Creating the Files••
สิ่งแรกที่เราจะต้องทำคือ เราจะต้องตั้งชื่อ Module โดยการตั้งชื่อโมดูลนั้น เราจะต้องให้มีความเหมาะสม
คือ ควรจะสั้นแล้วก็กระชับ สามารถเข้าใจได้ว่าโมดุลของเราจะทำอะไร ต่อมากคือ
เราจะใส่โมดุลที่เราสร้างไว้ใน sites>modules>วางโมดุลลงไป(โฟลเดอร์) โดยจะต้อง FTP เอา
โดยการเก็บโมดูลนี้จะแยกจาก Core ไฟล์ที่เกี่ยวข้องกับ module ก็จะอยู่ภายในโฟลเดอร์โมดุลของเรา
และเราควรจะมีไฟล์ readme.txt เพื่อบอกให้ผู้ใช้รู้เกี่ยวกับการใช้งาน module ของเราด้วย
เราจะต้องสร้าง name.info ในที่นี้ name คือโมดูลของเรา
ภายในไฟล์ .info นี้ก็จะมีลักษณะ
; $Id$
name = Annotate
description = Allows users to annotate nodes.
package = Example
version = “$Name$”
name = ชื่อโมดูล
description = คำอธิบายเกี่ยวกับโมดูล
package = บอกให้รู้ว่ามอดูลเรามีเพื่อนร่วมมอดูลอะไรบ้าง ฃ
version = บอกว่า Drupal ที่เราใช้ Version ไหน
ไฟล์ต่อมาที่เราต้องทำการสร้างคือ annotate.module ภายในโฟลเดอร์ย่อยannotate
เราจะเริ่มด้วยโค้ด PHP โค้ด … ๖๗
โดยคำบรรยายอธิบายโมดูลจพอยู่ในเครื่องหมาย /*** และปิดด้วย **/
โดยแต่ละบันทัดเราจะต้องขึ้นด้วย (*)
<?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.
- /
สุดท้ายจะต้องปิดด้วย (?>) และ save file แล้วก็เข้าไปที่
Administer ? Site building ? Modules.
moduleของคุณก็จะโชว์ใน list แล้ววว
ส่วนการตั้งค่าให้เราสามารถใช้webfrom ได้ มี 2 ขั้นคือ
1. เราจะกำหนด path ที่เราจะเข้าไปเซ็ทค่าของเราได้
2. เราจะสร้างรูปแบบการตั้งค่า
(อันนี้แปลแล้วงง)
••Implement a hook••
เราจะย้อนกลับที่การสร้างระบบของhook ซึ่งในบางครั้งเราจะเรียกว่า Callback.
เวลาที่เราทำงาน drupal จะถามว่าเราต้องการจะทำอะไร เช่น เมื่อเราต้องการโมดุลที่ตอบสนองคำร้องขอปัจจุบัน
มันจะจัดการหาเส้นทาง และจะแสดงเป็นList มาให้ และเรียกฟังก์ชั่นที่มีชื่อโมดูลมาให้
เช่น plus_menu => annotate_menu (งงวุ้ว)
/**
- Implementation of hook_menu().
- /
function annotate_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
‘path’ => ‘admin/settings/annotate’,
‘title’ => t(‘Annotation settings’),
‘description’ => t(‘Change how annotations behave.’),
‘callback’ => ‘drupal_get_form’,
‘callback arguments’ => array(‘annotate_admin_settings’),
‘access’ => user_access(‘administer site configuration’)
);
}
return $items;
}
User จะเข้าไปใช้งานใน admin>setting>annotae และเรียกฟังก์ชั่น Drupal_get_form()
โดยมี annotate_admin_set เป็นพารามิเตอร์ ส่งไปด้วย
โดยข้อความที่เป็น title จะต้องอยู่ภายใน t()ส่วน description ก็เช่นกัน
••Adding Module-Specific Settings••
Drupal มีnode หลายชนิดเช่น story & page หากเราต้องการที่จำกัดการใช้งานไว้เพียงบางโมดูล
(คงจะประมาณว่า Node บางอัน user ธรรมดา อาจไม่เหน)
เราก็จะต้องสร้าง page ซึ่งเราจะทำให้มันสามารถบอก โมดูลได้ว่าเป็น node ชนิดไหน เราต้องแอดข้อมูลด้านล่างลงไป
/**
- Define the settings form.
- /
function annotate_admin_settings() {
$form['annotate_nodetypes'] = array(
‘#type’ => ‘checkboxes’,
‘#title’ => t(‘Users may annotate these node types’),
‘#options’ => node_get_types(‘names’),
‘#default_value’ => variable_get(‘annotate_nodetypes’, array(’story’)),
‘#description’ => t(‘A text field will be available on these node types to make
user-specific notes.’),
);
$form['array_filter'] = array(‘#type’ => ‘hidden’);
return system_settings_form($form);
}
เราจะให้ชื่อของ form แสดงข้อความอยู่ใน t() เราจะตั้งค่าการแสดงผลของ node_get_types(‘name’)
ให้คืนค่าได้ง่ายมากยิ่งขึ้น
••Adding the Data Entry Form••
การเพิ่มdata form
/**
- Implementation of hook_nodeapi().
- /
function annotate_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case ‘view’:
global $user;
// If only the node summary is being displayed, or if the
// user is an anonymous user (not logged in), abort.
if ($teaser || $user->uid == 0) {
break;
}
$types_to_annotate = variable_get(‘annotate_nodetypes’, array(’story’));
if (!in_array($node->type, $types_to_annotate)) {
break;
}
// Add our form as a content item.
$node->content['annotation_form'] = array(
‘#value’ => drupal_get_form(‘annotate_entry_form’, $node),
‘#weight’ => 10
);
}
}
เราจะเรียกใช้ nodeapi() ก็ต่อเมื่อ Drupal มีการทำงานหลายอย่าง หรือมี implement hook อื่นๆ
module อื่นๆ จำเปลี่ยนแปลงnode ก่อนการทำงานที่ต่อเนื่อง เราจะได้รับNode ผ่านตัวแปร $node
และ ‘&’ ในค่าตัวแปรนั้น จะแสดงว่าเป็นการอ้างอิงไปยัง $node object ซึ่งจะหมายความว่า
การเปลี่ยนแปลงใดๆที่เกิดขึ้นกับ node จะถูกเก็บไว้ทั้งหมด แล้วเราก็จะสามารถทำการเปลี่ยน node ได้
การส่งข้อมูลบางส่วนก็จะใช้การเรียนฟังก์ชั่น ข้อมูลที่อยู่ในตัวแปร &op ในการส่งค่ามาว่า
เราต้องการที่จะทำอะไร เช่น Insert ,delete,modify โดยเราจะใช้ Swich case
ในการเลือก case ของค่าที่มากับตัวแปร $op
และเมื่อตัวแปร $teaser (codeด้านบน) มีค่าเป็นจริงถ้าเป็นแบบนี้ node ก็จะไม่แสดงเอง แต่จะแสดงเป็นlistแทน
เช่น ผลลัพธ์ของการเสิรช เราจะไม่สนใจการเพิ่มในกรณีนี้ แต่ในกรณีอื่นๆ หาก $user = 0 ก็จะหมายความว่า
ไม่มีใครlogin อยู่เลย และเราจะทำการออกจากswitch case โดยการใช้break
ก่อนที่เราจะทำการเพิ่ม annotation form ไปยัง webpage
เราจำเป็นต้องตรวจสอบว่า node ไหนกำลังทำงานอยู่บ้าง เราต้องเก็บสถานะการเปลี่ยนแปลงไว้ในอาเรย์
ของnode เอาไว้ทำการ implement แล้วก็ติดตั้ง hook & เก็บค่าตัวแปร
สุดท้ายเราจะต้องสร้าง ฟอร์มและ เพิ่มมันเข้าไปในcontent ของ$node อย่างแรกคือ เราต้องกำหนดค่าฟอร์ม
ซึ่งเราถ้าเรามีบางอย่างจะต้องเพิ่มเข้าไป เราจะต้องแยกฟังก์ชั่นให้มันด้วย ดังเช่น
/**
- Define the form for entering an annotation.
- /
function annotate_entry_form($node) {
$form['annotate'] = array(
‘#type’ => ‘fieldset’,
‘#title’ => t(‘Annotations’)
);
$form['annotate']['nid'] = array(
‘#type’ => ‘value’,
‘#value’ => $node->nid
);
$form['annotate']['note'] = array(
‘#type’ => ‘textarea’,
‘#title’ => t(‘Notes’),
‘#default_value’ => $node->annotation,
‘#description’ => t(‘Make your personal annotations about this content
here. Only you (and the site administrator) will be able to see them.’)
);
$form['annotate']['submit'] = array(
‘#type’ => ’submit’,
‘#value’ => t(‘Update’)
);
return $form;
}
เราจะสร้างฟอร์มเหมือนกับการสร้าง annotate_admin_settings() โดยเราจะสร้างตัวแปรอาเรย์มา
ซึ่งภายในอาเรย์นั้นจะต้องมี textbook / submit โดยเราจะต้องจัดกลุ่มให้ textbook / submit เข้าด้วยกันบนเว็บเพจ
อันดับแรกเราจะต้องสร้าง อาเรย์ที่เป็น #type=>’fieldset’ และตั้งชื่อให้มัน(fieldset) โดยค่าของtextbook / submit
ก็๋จะเปนสมาชิกของfieldset(อาจจะหมายถึงอาเรย์ซ้อนอาเรย์) และเราจะใช้$form['annotate']['note']แทน $form['note'].
วิธีนี้ Drupal จะสามารถเข้าใจได้ว่า textarea เป็น สมาชิกของ fieldset