Disabling the node page

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.

<?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.

How to use your snippet? I put this code to document type “page”, enabled php code filter but there are no changes. I can still see node page. Thank you.

This snippet is really for advanced users. It requires that you create your own module. It will not work if you embed this code into a node with the PHP filter turned on.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Adds typographic refinements.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
Please help us keep out spam by typing the text in the image.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.