[dctpe2010] drupal 模組開發入門
DESCRIPTION
Drupal 提供了非常大的彈性,加上社群所貢獻的大量模組,許多的功能似乎不需要動到程式碼就能夠達成。 不過隨著網站規模的擴大,或是個別需求的複雜化,透過操作來產生功能的形式遇到了操作繁雜或是執行效能不彰的問題,這也許就是進一步了解 Drupal 程式設計的時候了。 在這個議題中,主要針對如何透過程式設計技巧來完成所需要的功能,讓參與的朋友可以對於模組開發有些基本的認知。TRANSCRIPT
![Page 1: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/1.jpg)
Drupal 模組開發By kiang
![Page 2: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/2.jpg)
站在台上這個 ...
●就這間電腦工作室( http://olc.tw )●台灣 PHP 聯盟( http://twpug.net )●... 其他不為人知的過去
![Page 3: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/3.jpg)
為什麼
要自己製作模組?
![Page 4: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/4.jpg)
1. 減少重複操作
![Page 5: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/5.jpg)
Drupal 是少數有驚人數量操作組
合的系統
![Page 6: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/6.jpg)
2. 簡化開發邏輯
![Page 7: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/7.jpg)
我會寫 PHP
![Page 8: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/8.jpg)
但我並不清楚數以千計模組怎麼
來的
![Page 9: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/9.jpg)
3. 執行效率
![Page 10: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/10.jpg)
一個查詢搞定
![Page 11: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/11.jpg)
就沒必要用一堆查詢
![Page 12: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/12.jpg)
模組的基本結構
![Page 13: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/13.jpg)
my_module.info
![Page 14: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/14.jpg)
name = My Moduledescription = This is my modulecore = 6.x
![Page 15: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/15.jpg)
my_module.module
![Page 16: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/16.jpg)
<?phpfunction my_module_menu() { $items['my_module'] = array( 'title' =>'My Module', 'page callback' =>'my_module_page', 'access arguments' =>array('access content'), 'type' =>MENU_NORMAL_ITEM ); return $items;}
function my_module_page() { return 'Hello, this is my module';}
![Page 17: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/17.jpg)
.
![Page 18: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/18.jpg)
.
![Page 19: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/19.jpg)
放個資料表
![Page 20: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/20.jpg)
my_module.install
![Page 21: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/21.jpg)
<?phpfunction my_module_install() { drupal_install_schema('my_module');}function my_module_uninstall() { drupal_uninstall_schema('my_module');}function my_module_schema() { $schema = array( 'my_module' =>array( 'fields' =>array( 'id' =>array('type'=>'serial'), 'name' =>array('type'=>'varchar', 'length'=>'255'), 'email' =>array('type'=>'varchar', 'length'=>'255') ), 'primary key' =>array('id') ), );
return $schema;}
![Page 22: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/22.jpg)
my_module.module
![Page 23: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/23.jpg)
function my_module_page() { $result = db_query('select * from my_module'); $rows = array(); while ($data = db_fetch_object($result)) { $rows[] = array($data->id, $data->name, $data->email); } $headers = array('ID', 'Name', 'Email'); return theme('table', $headers, $rows);}
![Page 24: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/24.jpg)
.
![Page 25: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/25.jpg)
.
![Page 26: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/26.jpg)
.
![Page 27: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/27.jpg)
加個表單
![Page 28: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/28.jpg)
function my_module_form( & $form_state) { $form = array(); $form['name'] = array( '#type' =>'textfield', '#title' =>t('Name'), ); $form['email'] = array( '#type' =>'textfield', '#title' =>t('Email'), ); $form['submit'] = array('#type'=>'submit', '#value'=>'Submit'); return $form;}
function my_module_form_submit($form, & $form_state) { db_query('insert into my_module values (null, \'%s\', \'%s\')', $form_state['values']['name'], $form_state['values']['email']); drupal_goto('my_module');}
![Page 29: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/29.jpg)
return theme('table', $headers, $rows) .
drupal_get_form('my_module_form');
![Page 30: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/30.jpg)
.
![Page 31: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/31.jpg)
刪除資料
![Page 32: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/32.jpg)
function my_module_page($arg = 0) {$arg = intval($arg);
if($arg > 0) { db_query('DELETE FROM my_module WHERE id = %d', $arg); drupal_set_message('Data removed!!!');
} $result = db_query('select * from my_module'); $rows = array(); while ($data = db_fetch_object($result)) { $rows[] = array($data->id, $data->name, $data->email, l('Delete', 'my_module/' . $data->id) ); } $headers = array('ID', 'Name', 'Email', 'Action'); return theme('table', $headers, $rows) . drupal_get_form('my_module_form');}
![Page 33: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/33.jpg)
.
![Page 34: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/34.jpg)
以上只是玩玩 !!
![Page 35: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/35.jpg)
還要做更多
![Page 36: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/36.jpg)
資料檢查、顯示表格的分頁、區分使用者權限、將顯示分離 ( 樣板 ) 、抽離商業邏輯 (MVC) 、建立自訂內容類型、使用分類系統、加入 javascript 、與其他模組互動、加入使用者可調整設定、多國語言、產生區塊內容、使用佈景元素、系統排程、指令模式、開放介面、技術文件、操作手冊、教育訓練、功能維護、版本控制、需求轉換、程式碼的再運用、有事沒事去
看看新模組、新架構、新版本、 ......
![Page 37: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/37.jpg)
認命吧!
![Page 38: [DCTPE2010] Drupal 模組開發入門](https://reader033.vdocuments.net/reader033/viewer/2022050815/546e787baf7959ed6d8b68aa/html5/thumbnails/38.jpg)
下一位 ;)