Sunday, February 14, 2010

Showing left and right region on page not found + DRUPAL

Drupal has it's own system to handle "Page not found" errors. It uses a simple function, called drupal_not_found to set the correct headers, etc. That function calls theme_page to do its rendering.
The catch here is that it calls theme_page with the $show_blocks argument set to FALSE. This means blocks in the left and right columns won't be shown here.

-> print theme('page', $return, FALSE);

In some cases, you might not want this behavior. You might have your navigation in one of these sidebars and you want to give your visitors some way out of this page. The navigation might get the person back on track.
How can we fix this? Very simple. Put the following code in your page preprocessing function.

->function phptemplate_preprocess_page(&$variables) {
  // show all regions if page not found
  $page_not_found = strpos(drupal_get_headers(), 'HTTP/1.1 404 Not Found') !== FALSE;
  if (!$variables['show_blocks'] && $page_not_found) {
    global $theme;
    // Populate all block regions.
    $regions = system_region_list($theme);
    // Load all region content assigned via blocks.
    foreach (array_keys($regions) as $region) {
      // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
      if ($region == 'left' || $region == 'right') {
        $blocks = theme('blocks', $region);
      // Assign region to a region variable.
      isset($variables[$region]) ? $variables[$region] .= $blocks : $variables[$region] = $blocks;

It really works for me.

1 comment:

  1. I was looking for the same concept and finally got it. It is really written it in simple manner and very understandable.. thank U