1

I am trying to implement the solution given in this question.

I want to have custom templates for 4xx pages.

I am using Drupal 8.3.1

I have tried the code as given in the above link to create the page suggestions in my theme's .theme file:

/**
 * Implements hook_theme_suggestions_page() to set 40x template suggestions
 */
function MY_THEME_NAME_theme_suggestions_page(array $variables) {
  $path_args = explode('/', trim(\Drupal::service('path.current')->getPath(), '/'));
  $suggestions = theme_get_suggestions($path_args, 'page');
  $http_error_suggestions = [
    'system.401' => 'page__401',
    'system.403' => 'page__403',
    'system.404' => 'page__404',
  ];
  $route_name = \Drupal::routeMatch()->getRouteName();
  if (isset($http_error_suggestions[$route_name])) {
    $suggestions[] = $http_error_suggestions[$route_name];
  }

  return $suggestions;
}

It does not appear to work.

page--404.html.twig or page--system--404.html.twig are not being used.

404 and 403 pages are not set inside the admin of the page.

What else am I suppose to do to get this working?

  • I am getting ready to do something similar and was researching this, and I see folks using HOOK_theme_suggestions_HOOK_alter() api.drupal.org/api/drupal/… so, I wonder if you just need to add _alter to your function name. Also, it's always worth mentioning, did you 'clear all caches' under admin->config->devel->performance? – rjl Jun 7 '17 at 16:42
  • Sure have, many times. No luck. – tanbog Jun 8 '17 at 5:09
0

I have pretty much the same code as you have with the exception that I have a hook_theme_suggestions_page_alter(). With the _alter attached to it.

Try putting the HOOK in a custom module instead of the .theme file.

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.