Useful Tips For Custom Module Development In Drupal

Here are the few tips that is useful while developing any Drupal custom module,

1) Write required information and small description into modulename.info file, like Module Name, Package Name and Version Information and Dependencies, Name, Description and Package should be your project related so you can identify your custom module easily.

2) Create modulename.install file if your module required some one time operations, in modulename.install file use hook_install() to create tables or store value in database that is required by your module also write hook_uninstall() and remove/drop tables from the database when you uninstall the module. also use variable_del() function in hook_uninstall() to delete all variables that are used in your module.

3) Create one folder in your modules directory like “YourProject_Modules” which contains all custom modules in “YourProject”

4) Write appropriate comment before every hook or function.

5) Write all code in hook or function : don’t write inline code for eg. if you are adding css/js from the module .. then use hook_init() or other appropriate hook. Dont add it from anywhere in the module  and use proper function like drupal_add_js() / drupal_add_css()

6) Rather then fetching information from query try to find out any function available in drupal api. and use form api for creating HTML forms.

7) Make habit to write theme function for the output of any function, define all theme function in hook_theme() in top of module. and use functions like theme_links()theme_table() to represent links and table.

8) Never use static path or any static value in module, create your settings form for your module in which you can gather all information from interface and store database, so you can use/change anytime, use system_settings_form() (http://api.drupal.org/api/function  system_settings_form) function for creating settings form in your admin/settings section.

9) Never use Direct queries for updating node/taxonomy/comment always use api function, it will help in rules and other api related actionsfor eg. of node_save() function …

insted of using update query like…

db_query(“UPDATE {node} SET status = %d where nid = %d”,1,$nid);

please use:

$node = node_load($nid);

$node->status = 1; // or 0 for unpublish

node_save($node);

Please use same thing to change value of taxonomy or cck field value in node.

10) Always use api function l() for the link and t() for the text.

11) Always follow coding standards of Drupal ( http://drupal.org/coding-standards )

12) Don’t forget to write hook_update() in your install file if you have any database update in your newer version.

14) Finally never close the “?>” php tag at the end of the module 🙂 many times it will give blank page in site.

Some useful links for the module development:

API : http://api.drupal.org

Module Development Guide on Drupal.org : http://drupal.org/developing/modules

Coding Standards : http://drupal.org/coding- standards

Global Variables : http://api.drupal.org/api/globals

Leave a Comment