How to check, in D7, if the current page is returning a 404 error from a module/template?
3 Answers
In Drupal 7, you can use drupal_get_http_header()
.
In the template.php file, use this code.
$status = drupal_get_http_header("status");
if ($status === '404 Not Found'){
// Do something.
}
In Drupal 8, you can use the following code in a hook.
$route_name = \Drupal::request()->attributes->get('_route');
if ('system.404' === $route_name) {
// Do something.
}
-
What function does this code go in, within template.php? Commented Nov 20, 2012 at 8:13
-
1You can place this code inside template_preprocess_page(&$variables)– fzmasterCommented Nov 26, 2012 at 14:46
-
-
If you're looking to handle a 403, you can use:
if ($status == '403 Forbidden') { /* ... do stuff ... */ }
Commented Mar 27, 2015 at 22:59 -
2Note: the solution for Drupal 8 based on the
system.404
route does not work if you've specified a node page as your 404 page in Configuration > System > Basic site settings . See @Gervase' answer for a solution that works when you either have or do not have a custom 404 page specified. Commented Mar 8, 2018 at 17:40
Drupal 8.2.x:
Unfortunately, drupal_get_http_header("status") doesn't work any more.
Try:
$status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
if ($status && $status->getStatusCode() == 404){
}
There is a discussion of this here: https://www.drupal.org/node/1969270
-
1
-
1Careful though - the object retruned there might not have a
getStatusCode
function.– fritzmgCommented Mar 22, 2018 at 17:45 -
-
This is the simplest way to detect Access Denied (403) and Page Not Found (404) in Drupal 7.
// get the menu router item for the current page
$router_item = menu_get_item();
// if there is no router item, this page is not found
$is_page_not_found_404 = empty($router_item);
// if 'access' is empty for the router item, access is denied
$is_access_denied_403 = empty($router_item['access']);
-
$router_item
won't be empty if thesite_404
variable is set to a node path, so additional checks would be needed.– gappleCommented Dec 10, 2014 at 1:18 -