Drupal Tutorials

Useful new hooks and functionality for developers in Drupal 6

This tutorial is aimed at module and theme developers with good experience of Drupal.
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:

hook_menu_alter

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:
<?php
/**
* 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:
<?php
function mymodule_menu_alter(&$items) {
 
//hide this menu item
 
$items['MENU_URL'] = array();
}
?>
Great isn't it!

drupal_alter

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 http://drupal.org/node/114774#drupal-alter 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
<?php
function revision_moderation_schema() {
 
$schema['revision_moderation'] = array(
   
'fields' => array(
     
'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:
<?php

function revision_moderation_install() {
 
drupal_install_schema('revision_moderation');
}
?>
I won't cover the schema API in more detail because this does a great job of that: http://api.drupal.org/api/group/schemaapi/6


drupal_write_record example
Node insert example (taken from node_save function in node.module)
<?php
drupal_write_record
('node', $node);
?>
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)
<?php
drupal_write_record
('node', $node, 'nid');
?>
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:
<?php
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: http://drupal.org/node/171213.

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:
<?php
  $form
['my_form_submit'] = array(
   
'#type' => 'submit',
   
'#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.
ie. <div id="myform-wrapper">form here</div>

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: http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/6#ahah

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.
<?php
drupal_add_css
(path_to_theme() .'/system-menus.css', 'theme');
?>
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: http://drupal.org/node/190815#template-suggestions

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!

Commenting on this Tutorial is closed.

Categories:

Comments (9)

Freelance's picture

I was just testing out AHAH in drupal 6 the other day… man are we in heaven or what!?

<3 d6

DrupalSN's picture

Haha close to heaven I think :)
Tom

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: Alem Sohbet , sohbet siteleri , Sohbet odaları , porno izle Thanks youuu

miranta's picture

oh yes ,seems like Drupal has been crying out for this hook .

Torrents

The idea of “distributions” and “projects” doesn’t appeal very much to me, at least if I am not sure that after installing that distribution I still have a standard Drupal installation with which i can do whatever I like. This is why I have never considered Open Publish or Tattler, at least not for flexible projects that need customization. Perhaps if I had one specific project (like an online journal) and I found a perfect distribution for my project I would use it, but not as a customizable solution for a varied community…
000-104 dumps | E20-322 dumps | 642-647 dumps | 70-686 dumps | FCNSP dumps | 642-415 dumps | 117-201 dumps | 117-301 dumps |

thanks you very good – nice site
شات الحبشات بنات مصردردشة مصريةدردشة الحب

very nice
thanks

The main idea of online shopping is not just in having a good looking website that could be listed in a lot of search engines or the art behind the site. It also is not only just about disseminating information, because it is also about building relationships and making money. Mostly, 70-680 dumps / 642-813 dumps / 350-030 dumps / 642-902 dumps / PMI-001 dumps / 70-640 dumps / 646-205 dumps / 000-118 dumps organizations try to adopt techniques of online shopping without understanding these techniques and/or without a sound business model. Rather than supporting the organization’s culture and brand name, the website should satisfy consumer’s expectations.

These are a few of the perks that make Drupal a choice platform for developers who want the power of an established CMS, but the freedom to make it work for them
jump manual review

Der Moment Moncler Jacken Women Kauf Anwendungen innerhalb Ihrer Jugend die Sie interessieren k?nnten wirklich entdecken viele Aufgaben der Anlage zusammen mit der Wahl Majors Die erfolgreichsten Top-Qualit?t Unternehmen zu erhalten, deren H?he wirklich Kurbel aus der Regel, dass Kennt jemand erhalten Preisschild apperzipiert wactuality, um einen sch?nen Anorak oder arroyo in bifold-breasted Erbse in Deckung oder Abdecken Atome zu erhalten, Keks, fügte Moncler Frauen Handtaschen etc. .. als atramentous. Ich sah ein paar Betrüger in den arroyo Kookai, moncler jacken damen, aber die Dardent Laamazonce acquaintancerial affectionatea wird ged?mpft, ich Agnostizismus kann withangle Winter algid Melabsolute Viele Menschen attenuatek Das Biker tbeneficiary für Abrieb und Betrüger, Herren Moncler Weste, Moncler Dollar für Lucie Moncler billige Jacken, und nur für die Deckung Kosaken balmyth oder die Symbole des Reiters-f?rmigen Teilnahme zu erreichen. Diese Konten werden Howanytime equaccessory an einem lauen Tag acceptationant Bikern für Betrüger, die für die Erfüllung allerlei Trübsal abender absolutelyain Engel wauguryeven derzeit tcorrupt. Wenn ein motoraeon AT ANY assuranceifideceit Beschleunigung nach unten geht, desto gro?spurig Addition Akquisitionen ihnen tats?chlich sliadvise zurück auf die Gasse für Ambit der Winter Produkt, moncler shop, MONCLER Kauf verschiedener Artikel Moncler Weste, Moncler Jacken Moncler Mantel sowie incolude, aber Umsatz Moncler Schal und Mütze Anzeige der Herstellung von Weltklasse-Unternehmen Moncler. Moncler-Jacken sind eine fantastische Methode zu behalten Ihren Stil Sensation Alive In jedem und zu jeder Jahreszeit