hooks

Extend node filters form

I've been wanting to extend the default node_filter_form on admin/content/node since ages so I finally wrote my own patch because I really needed it for some projects. The patches attached introduce a new hook called hook_node_filters. Modules can hook into the default node_filters function to add their own select boxes and types to filter on. An example hook could look like this:

<?php
/**
* Implementation of hook_node_filters().
* Returns extra filter option at admin/content/node to filter on every page.
*/
function testmodule_node_filters() {
  
$q = "SELECT n.nid, n.title FROM {node} n WHERE n.type = 'page'";
  
$result = db_query(db_rewrite_sql($q));
   while (
$row = db_fetch_object($result)) {
    
$options[$row->nid] = $row->title;
   }
   return array(
'page' => array(
      
'title' => 'page',
      
'options' => $options,
      
'where' => 'n.nid = %d',
      
'join' => '')
   );
}
?>

Although I'm pretty sure this patch is safe, this is not an official patch reviewed by the Drupal community. In the event something might go wrong, don't hold me responsible allright ? ;) Also, take a look at http://drupal.org/project/better_node_admin_content which will hopefully serve as a patch for D7.

Topics 

drupal, hooks

The init hook enigma, part 2

I struggled a few days with some functionality on the new CarChannel site where visitors are offered a splash page the first time they visit the site. I needed to built in some exceptions, like the special msn page, the xml calls and the game pages. Testing when you are logged in always worked, but when browsing anonymous and with normal caching on, things went wrong, especially with url aliases. Reading source code and documentation - or now when you are reading this article - can save your life, so when you have an init hook and you need the know the 'q' variable, add

drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);

at the top of your hook before doing anything else. This will save you many sleepless hours and frustrating tests!

Being the only *nix sysadmin (or at least, the only guy at work who knows a little more then just opening an ssh-session) also made me very stressed out the last couple of weeks because services suddenly died without leaving any information in logs. I spent the whole afternoon yesterday setting up extra monitoring services on 2 higly imported webservers which check apache, mysql, ssh and load average every five minutes and , if needed, takes the appropriate action (restart, renice etc). It was the first night in a week I finally slept the whole night through, so fingers crossed!

Topics 

drupal, work, linux, hooks

The init hook enigma

I wasted a few hours on figuring out why an init hook in one of my modules wasn't invoked when normal page caching was on. Turns out the boostrap field in the system table for my module was still 0, instead of 1. So remember boys and girls, when you allready enabled your own custom module and you add an hook_init, remember to disable and enable your module again so Drupal knows it exists. Or manually alter the record in the system table in your database if disabling the module breaks your site.

On the bright side, I think I know every line of bootstrap.inc and cache.inc file now by heart.

Topics 

drupal, hooks
Subscribe to RSS - hooks

You are here