Useful new hooks and functionality for developers in Drupal 6

Useful new hooks and functionality for developers in Drupal 6

Since the official release of Views 2 and now CCK for Drupal 6 I’ve had the pleasure of using Drupal 6 a lot more in my day job.

So I thought I’d share some of the new hooks and functionality I’m finding really useful in Drupal 6:
Seems like Drupal has been crying out for this hook forever and now it’s here.
What it does
Like the hook_form_alter but it allows you to alter menu items even core module ones. You can override all aspects of a menu item defined by modules including the menu page callback, the menu item title and even the menu item access checking.
This is really useful if you want to override the way another module does something and none of the other alter hooks (form alter, link alter) can help.
For example I was recently working on a moderation system and we wanted to override the display of the node revisions list and all I had to do was add the following code to reroute the menu callback to my custom function:

  • Implementation of hook_menu_alter().
    function mymodule_menu_alter(&$items) {
    //reroute node revisions overview page
    $items[‘node/%node/revisions’][‘page callback’] = ‘rerouted_node_revision_overview’;


  • My custom version of node_revision_overview
    function rerouted_node_revision_overview($node) {
    You can even hide certain unwanted menu items/pages defined by other modules:

    Great isn’t it!
    Developers building and manipulating standard Drupal data structures in their modules can expose those structures for manipulation by other modules using the drupal_alter() function.
    See for an example.
    Schema & drupal_write_record
    What they do
    The new database schema has simplified inserting and updating items in your database.
    You define your database schema in your modules .install file (see example below) then you can simply call the new drupal_write_record function telling it what database table to write to and the information to write and the function will do the rest. SO you’ll find yourself writing less and less of those long db_query insert and update commands!

Example of schema API taking from revision_moderation module
‘nid’ => array(
‘type’ => ‘int’,
‘unsigned’ => TRUE,
‘not null’ => TRUE,
‘default’ => 0
‘revision_moderation’ => array(
‘type’ => ‘int’,
‘not null’ => TRUE,
‘default’ => 0,
‘size’ => ‘tiny’
‘primary key’ => array(‘nid’),

return $schema;
Now hook_install in your .install files are even easier:

function revision_moderation_install() {
I won’t cover the schema API in more detail because this does a great job of that:

drupal_write_record example
Node insert example (taken from node_save function in node.module)

This would insert all the data in the $node object into the node database table.

Node update example (again taken from node_save function in node.module)

This would update the node in the node database table with the data in the $node object where nid is equal to the $node->nid.
Javascript Improvements
t() and format_plural() functions in Javascript
t (Translate) and format_plural functions now have a Javascript equivalents so you can now translate text within your javascript files.The parameter order is exactly like their server-side counterpart:
format_plural($count, $singular, $plural, $args = array(), $langcode = NULL);

t($string, $args = array(), $langcode = NULL);
Default script.js file for themes
Like the style.css files a new script.js will automatically be loaded by Drupal if one is present in your theme folder, so can now house and attach all your themes Javascript more easily.

Javascript Theme functions
Javascript files can now have theme functions which can be defined as new or you can override existing theme function just like the PHP theme layer.
There’s a nice example of the Javascript theme layer in action here:

AHAH Framework integration
The inclusion of the AHAH framework in Drupal 6 has made Ajax quicker and easier to implement.
The AHAH framework is fully integrated with the Drupal’s form API, here’s an example of how to use it:
‘#value’ => t(‘Submit’),
‘#weight’ => 1,
‘#submit’ => array(‘my_form_submit’),//none JS version
‘#ahah’ => array(
‘path’ => ‘myform/js’,
‘wrapper’ => ‘myform-wrapper’,
‘method’ => ‘replace’,
‘effect’ => ‘fade’,
So the form API should look familiar but let me explain the #ahah array piece by piece:

path: When the submit is click this is the menu callback that will be called in the background (without refreshing the page).
This will need to defined in your hook_menu function.

wrapper: This related to the ID of a HTML element on your page where the returned HTML from the path function is outputted.

form here

method: This is how the HTML returned from the path function should be attached to the wrapper element.
It can be one of ‘replace’ (default), ‘after’, ‘append’, ‘before’, ‘prepend’.

Effect: This is the effect that will be given to the HTML returned when it is outputed into the wrapper. In our example it would fade in.

For further detailed documentation see:

A more detailed AHAH tutorial is coming soon!
Override core CSS files
Themes can now override module defined CSS files by simply loading in a CSS file with the same name as the original.

This would replace the modules/system/system-menus.css with the theme version at mytheme/system-menus.css.
Theme improvements
Templates in subfolders
Your themes template files (.tpl.php) can now be put into subfolders and Drupal will automatically pick them up.
For example you could put all your node templates (node-NODE-TYPE.tpl.php) in a subfolder called node.

More template name suggestions
Extra theme template naming suggestions have been added to the theme engine, so you can now for example theme page.tpl.php based on node types ie. page-NODE-TYPE.tpl.php.
For a full list of the template suggestions see:

And that’s it! I know there are probably loads of great hooks and functionality new to Drupal 6 I haven’t mentioned, but these were some of my favourites so far!

Leave a Reply

Your email address will not be published. Required fields are marked *