Articles


[Drupal 7] Theming 403 and 404 error pages Drupal 7

[Drupal 7] Theming 403 and 404 error pages Drupal 7



Posted byUttam Kotekar,25th Jan 2016

Drupal 7 provides an interface to set URL for 403 and 404 error pages (admin/config/system/site-information). We can just simply create a node and set the path for this error pages. In this post, lets discuss about how we are going to theme this 403 and 404 error pages without affecting any other pages.

As you know about template suggestions, we will set "theme_hook_suggestions" variable to write our custom templates for 403 and 404 error pages. So now the questions arises: How are we going to set the variable before the page is processed? It is very simply, Drupal provides a theme preprocess hook which is trigger before the page is rendered. So we need to write the below piece of code in the template.php file.


/**
 * Implements hook_preprocess_Hook
 */
function THEMENAME_preprocess_page(&$vars) {
  Check for the header status
  $header = drupal_get_http_header("status");
  if ($header == "404 Not Found") {
    $vars['theme_hook_suggestions'][] = 'page__404';
  }
  elseif ($header == "403 Forbidden") {
    $vars['theme_hook_suggestions'][] = 'page__403';
  }
}

Create two files: page--404.tpl.php and page--403.tpl.php in your theme templates folder. Now, simply add your codes in these files and Check it out. Thats it :-)

Categories