I'd like to attach a library to a custom 404 page. I'd like to do this in my theme file. How can I know if the current response will be a 404 Page Not Found?


Got it.

In your hook within your theme file:

  $status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
  if ($status && $status->getStatusCode() == 404) {
    $page['#attached']['library'][] = 'themename/notfound';

Your Answer

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

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