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.
// To conserve CPU and bandwidth, omit the blocks.
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;
}
}
}
This will only render the left and right blocks, the other ones are already rendered by Drupal core.
Comments
hey i have 2 blocks in my left navigation/region how can i display only one block and not to display the other block ?
Correct. This post is about "unhiding" the sidebars (regions). In your own theme.
If you want to show navigation, that's another problem. Your navigation might not be in these sidebars. Best solution stays to add a custom 404 page to unhide your navigation.
Unfortunately, your solution is only partial because your body classes will still be wrong, showing "no-sidebars", when it could be showing 1 or 2 sidebars.
In addition, there are actually more problems with 404 pages than just missing left and right sidebars. The navigation links will often be AWOL.
I recently wrote the 404 blocks module to try to tackle all these issues. Give it a whirl!
Or you can simply rename the "left" and "right" regions from the .info file to something else. Then it will always show. That's the only thing drupal checks for in this situation so renaming them is an easier fix.
You will have to reassign your blocks in the block config page though.
Post new comment