Overriding Menu Page Callbacks

Sometimes you need to override more than a theme function: the entire page needs to be overridden. Drupal 6 makes it rather easy to do this.

In the example below, we call hook_menu_alter(&$callback) to override the display of every single node page view.

Instead of returning a node page, we return "[author's username] is great... for me to poop on.".

 

<?php
/**
* Implementation of hook_menu_alter().
* !!!!!!!!!!!!!must rebuild menus to take effect !!!!!!!!!!!!!
*/
function exampleModule_menu_alter(&$callbacks) {
 
$callbacks['node/%node']['page callback'] = 'fmtpo_page';
 
// it seems you have to override both when you're dealing with menu callback that is a default local task
 
$callbacks['node/%node/view']['page callback'] = 'fmtpo_page';
}

/**
*See "menu wildcards" if you are wondering how node made it into there.
*/
function fmtpo_page($node) {
 
// load the node's author's name -- this is personal...
 
$author = user_load(array( 'uid' => $node->uid));
  return
'<h3>'.$author->name.' is great...  for me to poop on</h3>';
}
?>

Discussion: Uses beyond telling authors they are great to poop on.

You can use this same technique to override anything defined in a menu array. The implication is that any page in drupal can be completely overridden -- whether its the menu callback, the access checking, or the page title.

The enterprising reader will no doubt think of applications of this technique that go far beyond this example.

Section: