Verizon cell phones

Creating Custom Drupal 6 Error Pages

Drupal logo
This article will explain how to create custom Drupal error pages. I will walk you through the entire set of steps and leave you with some code to fix up a common problem in the Drupal statistics module that many people run into.

Create Custom Drupal 403 and 404 Pages

The first thing we need to do is create custom pages for each HTTP error. We assume here that the Path module is enabled and clean URLs are enabled.

  1. Go to your Drupal login page and sign is as the administrator.
  2. Create two separate pages for your 403 and 404 errors. Type in the page body some useful information about the error to help your users.
  3. For the URL path setting type in error403 and error404 for each page.
  4. Save each page

Set Error Reporting Pages

Next we must let Drupal know about these custom error pages.

  • Click on the site configuration section
  • Click on the error reporting link
  • For the default 403 page, type in error403
  • For the default 404 page, type in error404
  • The error pages are the same name as what you specified in your URL path setting and must match.

    Test Your Error Configuration

    Type in some bogus URLs in your browser to see if the appropriate HTTP 403 and HTTP 404 error pages appear. If you did everything above correctly you should see your custom pages.

    Fixing the Statistics Block

    One common problem found by Drupal users is that if you have the statistics module on and use the Popular content block, you will see your custom error pages on the list. This is pretty much useless information to your users, especially if you get lots of 403 and 404 errors.

    To solve this problem we need to come up with some custom code to remove those 403 and 404 errors out of the rendering. Here is the PHP code to do just that:

    <?php
    function statistics_block2($dbfield, $dbrows) {
    $path = $nid = array();
    foreach (array(403, 404) as $err_code) {
    $path[$err_code] = drupal_get_normal_path(variable_get("site_$err_code", ''));
    if (substr($path[$err_code], 0, 5) == 'node/') {
    $nid[$err_code] = substr($path[$err_code], 5);
    }
    $nid[$err_code] = is_numeric($nid[$err_code]) ? $nid[$err_code] : 0;
    }
    if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) {
    return db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.uid, u.name FROM {node} n INNER JOIN {node_counter} s ON n.nid = s.nid INNER JOIN {users} u ON n.uid = u.uid WHERE s.$dbfield != 0 AND n.status = 1 AND n.nid != %d AND n.nid != %d ORDER BY s.$dbfield DESC"), $nid[403], $nid[404], 0, $dbrows);
    }
    return(FALSE);
    }
    ?>

    <?php
    function ShowStatisticsBlock() {

    if (user_access('access content')) {
    $daytop = variable_get('statistics_block_top_day_num', 0);
    if ($daytop && ($result = statistics_block2('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Todays:")))) {
    $content = $node_title_list;
    }

    $alltimetop = variable_get('statistics_block_top_all_num', 0);
    if ($alltimetop && ($result = statistics_block2('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) {
    $content .= $node_title_list;
    }

    $lasttop = variable_get('statistics_block_top_last_num', 0);
    if ($lasttop && ($result = statistics_block2('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) {
    $content .= $node_title_list;
    }

    if (count($content)) {
    return $content;
    }
    }
    }
    ?>

    The code above is modified from the discussion that went on the Drupal forum for filtering out 403 and 404 database records. This is a more complete solution that will show all statistical information in one block and test to make sure the user has appropriate content access.

    To use this code, simply make this call in your Drupal template or you can create a custom block:

    Popular Content

    <?php echo ShowStatisticsBlock(); ?>

    And there you have it! You now know how to create custom Drupal error pages and a way to get rid of having the 403 and 404 errors showing up in the statistics block.

    Filed under: