I want to check the status code of a page. I have tried with the following code in hook_preprocess_page(), but it doesn't work.

$status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception'); 
$status = \Drupal::request()->attributes->get('_route');
if ('system.404' === $route_name) {
  // Omissis.

How can I check the status code for a page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.