Writing a Drupal 5 module <ul><ul><li>Sammy Fung </li></ul></ul><ul><ul><li>(sammy@sammy.hk) </li></ul></ul><ul><ul><li>20...
Module Structure <ul><li>All contributed modules & themes </li></ul><ul><ul><li>sites/*/modules, sites/*/themes </li></ul>...
*.module includes... <ul><li>Custom Node Type </li></ul><ul><li>User Access </li></ul><ul><li>Database Operation (by itsel...
Custom Node Type (1/2) <ul><li>mymodule_node_info </li></ul><ul><ul><li>return array('mymodule' => array(...) </li></ul></...
Custom Node Type (2/2) <ul><li>mymodule_form(&$node) </li></ul><ul><ul><li>$form['fieldname1'] = array() </li></ul></ul><u...
User Access <ul><li>mymodule_acccess($op,$node) </li></ul><ul><ul><li>$op == create | update | delete </li></ul></ul><ul><...
Database Operation (module itself) <ul><li>mymodule_insert($node) - db_query(insert...) </li></ul><ul><li>mymodule_update(...
Database Operation (by other mods) <ul><li>mymodule_nodeapi(&$node,$op,$teaser,$page) </li></ul><ul><ul><li>called by  oth...
Menu items and page callbacks <ul><li>mymodule_menu($may_cache) </li></ul><ul><ul><li>$item = array(array()) </li></ul></u...
Others... <ul><li>mymodule_validate(&$node) </li></ul><ul><ul><li>validate user inputs if needed </li></ul></ul><ul><li>my...
Upcoming SlideShare
Loading in …5
×

Writing Drupal 5 Module

2,601 views

Published on

a overview of APIs for writing drupal 5 modules.

Published in: Technology, Art & Photos
  • Excellent overview of module development...
    A very concise and comprehensive...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Writing Drupal 5 Module

  1. 1. Writing a Drupal 5 module <ul><ul><li>Sammy Fung </li></ul></ul><ul><ul><li>(sammy@sammy.hk) </li></ul></ul><ul><ul><li>20 February 2008 </li></ul></ul>
  2. 2. Module Structure <ul><li>All contributed modules & themes </li></ul><ul><ul><li>sites/*/modules, sites/*/themes </li></ul></ul><ul><li>mymodule.info </li></ul><ul><ul><li>name, description, dependencies, package. </li></ul></ul><ul><li>mymodule.install </li></ul><ul><ul><li>install (create table), uninstall (drop table, delete). </li></ul></ul><ul><li>mymodule.module </li></ul>
  3. 3. *.module includes... <ul><li>Custom Node Type </li></ul><ul><li>User Access </li></ul><ul><li>Database Operation (by itself, other modules) </li></ul><ul><li>Menu items and page callbacks </li></ul><ul><li>Others </li></ul>
  4. 4. Custom Node Type (1/2) <ul><li>mymodule_node_info </li></ul><ul><ul><li>return array('mymodule' => array(...) </li></ul></ul><ul><ul><ul><li>name: human readable name of the node </li></ul></ul></ul><ul><ul><ul><li>module: prefix of module functions, eg. mymodule_ </li></ul></ul></ul><ul><ul><ul><li>description: brief description of the node type </li></ul></ul></ul>
  5. 5. Custom Node Type (2/2) <ul><li>mymodule_form(&$node) </li></ul><ul><ul><li>$form['fieldname1'] = array() </li></ul></ul><ul><ul><ul><li>'#type' => 'textfield' | 'textarea' | ... </li></ul></ul></ul><ul><ul><ul><li>'#title' => t('Field Name 1') </li></ul></ul></ul><ul><ul><ul><li>'#default_value' => $node->fieldname1 </li></ul></ul></ul><ul><ul><ul><li>'#size', '#maxlength', '#required' </li></ul></ul></ul><ul><ul><li>return $form; </li></ul></ul>
  6. 6. User Access <ul><li>mymodule_acccess($op,$node) </li></ul><ul><ul><li>$op == create | update | delete </li></ul></ul><ul><ul><li>return user_access('name of specified permission'); </li></ul></ul><ul><li>mymodule_perm() </li></ul><ul><ul><li>define a number of possible permissions (names) </li></ul></ul><ul><ul><li>return array('create content','edit own content'); </li></ul></ul>
  7. 7. Database Operation (module itself) <ul><li>mymodule_insert($node) - db_query(insert...) </li></ul><ul><li>mymodule_update($node) - db_query(update...) </li></ul><ul><li>mymodule_delete($node) - db_query(delete...) </li></ul><ul><li>mymodule_load($node) - db_query(select...) </li></ul>
  8. 8. Database Operation (by other mods) <ul><li>mymodule_nodeapi(&$node,$op,$teaser,$page) </li></ul><ul><ul><li>called by other modules to act on node </li></ul></ul><ul><ul><ul><li>load/insert/update/delete/delete revision </li></ul></ul></ul><ul><ul><ul><li>prepare/search result/print/submit/update index </li></ul></ul></ul><ul><ul><ul><li>validate/alter/rss feed/view </li></ul></ul></ul>
  9. 9. Menu items and page callbacks <ul><li>mymodule_menu($may_cache) </li></ul><ul><ul><li>$item = array(array()) </li></ul></ul><ul><ul><ul><li>'path': path to link to when the user selects the item. </li></ul></ul></ul><ul><ul><ul><ul><li>path = mymodule, admin/content/..., admin/setting/... </li></ul></ul></ul></ul><ul><ul><ul><li>'title' => t('my title'): title of menu item </li></ul></ul></ul><ul><ul><ul><li>'callback', 'callback arguments': call a function w/ arg </li></ul></ul></ul><ul><ul><ul><li>'access': user_access(...), can user access ? </li></ul></ul></ul><ul><ul><ul><li>'weight': integer, relative position of items in menu </li></ul></ul></ul><ul><ul><ul><li>'type': MENU_* </li></ul></ul></ul>
  10. 10. Others... <ul><li>mymodule_validate(&$node) </li></ul><ul><ul><li>validate user inputs if needed </li></ul></ul><ul><li>mymodule_view($node, $teaser = FALSE, $page = FALSE) </li></ul><ul><ul><li>simply runs the node text through the output filters. </li></ul></ul><ul><li>mymodule_help($section) </li></ul><ul><li>mymodule_ping($name='',$url='') </li></ul>

×