Common Drupal Functions for Module Development

There are a handful of functions that any module developer needs to know to get started developing for Drupal. In my perspective, functions fall into 4 categories: PHP functions (not specific to Drupal), general Drupal functions not associated with a specific module (such as database and error functions), hooks, and core module functions (often overwritten hooks). Module developers should always check to see if functions provided by required core modules (node, user) will fulfill their requirements before creating custom functions. If a developer needs just a small amount more functionality than a core module function provides (such as an additional permission), they should use a Drupal hook to append that functionality.

To use a Drupal hook, prefix the hook with your module name and create a function in your module. The hook function has the same parameters as its parent hook and only appends its functionality to that of the parent. (e.g. If your module is called 'example', you would name your function 'example_perm' with no parameters, and it would return an array of additional permissions for your module. It wouldn't change the already existing Drupal permissions.)

Hooks that any Drupal developer should be familiar with include hook_install, hook_uninstall, hook_access, hook_perm, hook_form_alter, and hook_menu at a minimum. If their module involves changing an existing node type, creating a node type, or nodes in general, the developer should also be familiar with hook_node_info and hook_nodeapi. The full list of hooks can be found on the drupal api site: http://api.drupal.org/api/group/hooks.

Drupal also has some all-purpose functions that will come in handy. These include database functions, general functions, and hook functions that were overwritten in required core modules. If there is a generic PHP function that has the same or similar functionality to a Drupal function, a Drupal developer should generally use the Drupal equivalent because it will be inherently more flexible. (It will not be exclusive to MySQL or PostgreSQL or a specific version of PHP for example.) Below is a list of functions that I use constantly. To view descriptions of these functions or to see examples of them in use, go to http://api.drupal.org and type the function name into the search.

Database functions:
db_query - execute a sql query
db_query_range - execute a sql query with a LIMIT
db_fetch_object - fetch single row of sql resultset as an object
db_result - fetch a single column of a single row (only use when resultset only has one value)

Access functions:
user_access - check if current user has specific permission
node_access - check if current user has a specific (create, view, update, delete) permission on a given content type

Message and error functions:
drupal_set_message - append a message to be displayed at the top of the next page (e.g. 'installation successful')
form_set_error - attach an error to a specific form element, end form submission, and display the error to the user

Other:
l - create a link to a relative path within the drupal site
node_load - get node object from node id
variable_get - check for the current value of a module-defined variable & set the value of the variable if not already defined