Drupal CustomError + PHP

On filbar.org I wanted a 404 error page that is helpful rather than just displaying an error that indicates the page has been blotted from existance. When I migrated my website to Drupal I had to radically change the organization of my website and some of the content didn't make it over. I have noticed that many of my "Page Not Found" errors were referencing the url's from the old site. My solution was to create a custom error page that offered a short explanation and a link to where the content might be located. I also wanted to include search results for the words in the URL. To see an example go to the following URL: www.filbar.org/Personal/Resume/Resume.php (no longer used on this site).

Using A Regular Page as a 404

My first attempt was to create a page entitled '404' and set page not found errors to that page. On the page I included php code to print the custom URL and the the search results. This worked extremely well but had two problems. Firstly the 404 page is counted as a real page and counts towards the statistics, my 404 page quickly became my #2 most popular article. I use a box that displays the top 5 articles and I didn't want my 404 error page on it! The second problem occured when you tried to access the page directly. The PHP code would go into some sort of loop and would not display.

Augmenting the CustomError Module For PHP

The customerror module solves these problems because it is not a normal page; however, it only evaluates the contents as HTML. Since I needed PHP for the custom link and search results to work I decided to customize the module to evaluate PHP code as well as HTML.

As it turns out it was very simple to modify the module, it only takes one extra line to process the contents as PHP. All that was needed was to pipe the contents of the error message through drupal_eval() before generating the error page. This is the function in the customerror module that generates the page with the added line:

/**
 * Implementation of hook_page().
 */

function customerror_page() {

  $error = arg(1);

  switch($error) {
    case 403:
    case 404:
      drupal_set_title(variable_get('customerror_'. $error .'_title',
        _customerror_fetch_error($error)));
      $body = variable_get('customerror_' . $error, _customerror_fetch_error($errror));

      // Added by Vince Filby to process the page as PHP
      $body = drupal_eval( $body );

      break;
    default:
      drupal_set_title(t('undefined error: ') . $error);
      $body = _customerror_fetch_error($error);
      break;
  }
  print theme('page', $body);
}

Once this is in place you can add whatever PHP code you like to your custom error pages. Here is the custom 404 error used on filbar.org:

The page you have requested is not found.  It is possible that it
was a page on my old website that either does not exist here or
exists in a difference location.  A direct link to the old website
is provided, but no guarantee if the content exists:

<div style="text-align: center;">
<a href="/<? print "old".$_SERVER['REQUEST_URI']; ?>">http://filbar.org/<? print "old".$_SERVER['REQUEST_URI']; ?></a></div>
<?

if (user_access('search content')) {
 
    $keys = implode( " ", explode( "/", $_SERVER['REQUEST_URI'] ) );
    $type = "node";

    // Only perform search if there is non-whitespace search term:
    if (trim($keys)) {

      // Collect the search results:
      $results = search_data($keys, $type);
     
      if ($results) {
          print "If that fails, here are some possible matches for the requested link:";
          print theme('box', t('Search results'), $results);     
      }
   }
}
?>

Read more...


Reply

The content of this field is kept private and will not be shown publicly.