Skip to main content
Category:

Code snippet that can be used to add custom 404 template file in Drupal 8.

<?php

/**
 * hook_theme_suggestions_page_alter().
 */
function mymodule_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $route_name = \Drupal::routeMatch()->getRouteName();
  switch ($route_name) {
    case 'system.401':
      $error = "page__401";
      break;

    case 'system.403':
      $error = "page__403";
      break;

    case 'system.404':
      $error = "page__404";
      break;
    default:
      $error= 'page__'.str_replace(".","_",$route_name);
      break;
  }
  if (isset($error)) {
    $suggestions[] = $error;
  }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook