Home Classroom Drupal An insight into the world of Drupal – Part 6

An insight into the world of Drupal – Part 6


Let’s build a module (Part 3)


In last two articles on Drupal, we discussed some basic steps in creating a module and how to setup the database tables. It’s now time to see actually how the module script should be written to implement the functionality required.

When it comes to writing the module script (.module file), the most important items are hooks. Drupal hooks are used to extend the functionality provided by the core modules and there are some key hooks that are essential in almost all modules we write.

1.Hook Menu – hook_menu($may_cache)

This hook enables modules to register paths, which determines whose requests are to be handled. Depending on the type of registration requested by each path, a link is placed in the the navigation block and/or an item appears in the menu administration page (q=admin/menu).
Drupal will call this hook twice: once with $may_cache set to TRUE, and once with it set to FALSE. Therefore, each menu item should be registered when $may_cache is either TRUE or FALSE, not both times. Setting a menu item twice will result in unspecified behavior.
For a detailed usage example, see page_example.module.

2. Hook Block – hook_block($op = ‘list’, $delta = 0, $edit = array())

Declare a block or set of blocks.
Any module can export a block (or blocks) to be displayed by defining the _block hook. This hook is called by theme.inc to display a block, and also by block.module to procure the list of available blocks.

3.Hook Perm – hook_perm()

Define user permissions.
This hook can supply permissions that the module defines, so that they can be selected on the user permissions page and used to restrict access to actions the module performs.

4. Hook Help – hook_help($selection)

Provide online user help.
By implementing hook_help(), a module can make documentation available to the engine or to other modules.

Now let’s see how these hooks can be integrated into a single module script to build our computer parts module.

First part is adding a hook menu which decides how to handle URLs coming to the module and where to direct them. Add this snippet to our previously created computer_store.module script.

In above code we have implemented our hook menu in computer store module. It defines two URL paths and what should be the action taken when each path was followed. These paths are ordered as a set of associative arrays and return at the end of the hook. Let’s try to understand the meaning of this menu array.

#title – Defines the title of the page to direct
#path – The path which the action should be taken upon visiting
#callback – This is the function that’s called when this path is followed
#callback arguments – The argument given here will be input to the function defined in #callback as an argument
#type – The type of this menu link
#access – What type of users can access this URL. Here we usually use user_access() function to check if a given permission is available to the current user. In above example, it checks if the current user has “’administer site configuration” privileges (which can be set from the Administer > User Management) and if not denies the access to such users.

For more information on hook menu check this URL.

Now add a simple form function named ‘computer_store_configure_form’ as defined by our earlier menu call back in our hook_menu. For the moment let’s just add a dummy form function and in next week let’s discuss thoroughly on how to improve our module using Drupal form API.

Now uninstall our module through Drupal module manager if it was previously installed and reinstall the module. Now you will notice that there’s a menu item in administer > Site configuration section named “ Computer Store” and clicking it will take you to the admin section of our newly created module.

Through this article we have seen how to setup a hook menu and in next article let’s see how to complete this module with Drupal Form API.



Laknath Buddhika is an old Royalist and currently doing undergraduate studies at Faculty of IT, University of Moratuwa. He has worked on various fields of IT for more than a decade and is currently employed at Vesess Pvt Ltd for more than 2 years. He is an avid enthusiast of Open Source and has participated in Google Summer of code programme twice(2007,2008) representing Open Source organizations Gnome, Eclipse and also a member and a module contributor for Drupal community. He has a keen interest in the PHP language and PHP based CMSs such as Drupal, WordPress and Frameworks like CakePHP and CodeIgniter. He is also employed at DMS Pvt Ltd as a part-time PHP lecturer and wishes to spread PHP and the idea of Open Source throughout the Sri Lankan society.


Leave a Reply