Disabling the node page

Note: this post contains code for Drupal 5 - if you're using Drupal 6, you'll have to adapt it to get it to work (this page might be a good place to start).

The default homepage for a new Drupal site is the /node page that lists all content types that are marked to be promoted to the homepage. It's easy enough to change the homepage to a certain node (like a simple page) or a more customized homepage.

But something that has always bugged me is that the /node page is still just sitting out there. At any point an informed user can type in the /node path on any of my Drupal sites and see a listing of much of the content on the site. Of course, we haven't taken any time to style such a page to render nicely and it just seems unprofessional to have page sitting out there that will never be used.

Overriding Core Paths

Recently, we decided to start doing something about these default pages that weren't being used. For all of our sizable Drupal projects we create a module specific to the project that is used for gluing our overall project together. One of frequently used hooks used in a Drupal module is the hook_menu() function. It is used to tie callback functions to URL paths.

Usually hook_menu() is used to create new paths, but it can also be used to override the action tied to paths defined in Drupal core modules. The code below shows how we use this function to override the /node path to return a 404 not found page instead the default node list.

[NOTE]This code is Drupal 5

<?php
function sitename_menu($may_cache) {

 
$items = array ();
  if (
$may_cache) {
   
$items[] = array (
     
'path' => 'home',
     
'title' => t('Homepage'),
     
'callback' => 'sitename_home',
     
'access' => TRUE,
     
'type' => MENU_CALLBACK,
    );
  }
  else {
   
$items[] = array (
     
'path' => 'node',
     
'title' => t('Not Found'),
     
'callback' => 'drupal_not_found',
     
'type' => MENU_CALLBACK,
    );
  }
  return
$items;
}
?>

You'll notice 2 menu paths returned here. The first is a path for our homepage (/home) tied to the function sitename_home(). The second is an override of the /node path that is tied to the drupal_not_found() function. You will this override path is called when $may_cache is false. The hook_menu() function is called twice, first with $may_cache set to TRUE early in the bootstrap process and second with $may_cache set to FALSE later in the bootstrap process. Placing our override in the latter ensures that cached menu items will be overridden successfully. If you are overriding non-cached menu items you may need to got the extra step of making sure your module menu items are invoked later in the bootstrap process by changing the weight of your module in the system table of your database.