2

We have a page that uses several themes, but the 404 pages should all look the same.

So, for all themes except one we need a rule that switches to another theme when the URL does not match any content.

There are ways to choose the theme by the current URL, like using the context module, or implementing a ThemeNegotiatorInterface, and we use them to assign our themes to certain parts of the website.

It seems that the ThemeNegotiatorInterface is not used when delivering a 404 error page.

So, how can we switch the theme according to the http status code?

1 Answer 1

3

I've done something similar by implementing the ThemeNegotiatorInterface. In the function that determines which theme should be used, the following code snippet checks for a 404 Page Not Found exception:

$status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
if ($status && $status->getStatusCode() == 404) {
  return 'name_of_the_theme_that_should_handle_404_responses';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.