Custom 404 Template File in Drupal 8

How can I create a custom one 404 pagein Drupal 8?

I created a new page (Content) in the back office called 404(node ​​number 100). I set it as the default 404 page in the " Configuration>" section of the Basic sitesettings Basic site.

Settings

It works with the content that I installed in Backoffice.

But now I want it to be edited programmatically, and I do not know how to create an override file for me.

I tried to create mytheme/templates/html--node--100.html.twigand it only works when this URL ( node/100) is requested directly , but it doesn’t work when you try to select a random URL slug and Drupal should allow this. When this happens, drupal provides me with the content that it 404 pagecontains in the back office, and not in the file that I just created.

I tried a few files, such as page--404-html.twig, html--node--404.html.twig, html--page--404.html.twig, ... but it does not work

Can anybody help me?

+6
source share
3 answers

page - system - 404.html.twig ( 4xx) Drupal 8.3, 4xx . https://www.drupal.org/node/2363987 , :

/**
 * Implements hook_theme_suggestions_page() to set 40x template suggestions
 */
function MYMODULE_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;
}

: , hook_theme_suggestions_page_alter . https://www.drupal.org/project/fourxx_templates ( https://github.com/ahebrank/fourxx_templates/blob/8.x-1.x/fourxx_templates.module)

+12

, , page - 404.html.twig , Drupal 8.5.1.

MYTHEME.theme

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_page_alter(&$suggestions, $variables, $hook) {
  /**
   * 404 template suggestion.
   */
  if (!is_null(Drupal::requestStack()->getCurrentRequest()->attributes->get('exception'))) {
    $status_code = Drupal::requestStack()->getCurrentRequest()->attributes->get('exception')->getStatusCode();
    switch ($status_code) {
      case 404: {
        $suggestions[] = 'page__' . (string) $status_code;
        break;
      }
      default:
        break;
    }
  }
}

page - 404.html.twig .


, switch.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_page_alter(&$suggestions, $variables) {
  /**
   * error page template suggestions.
   */
  if (!is_null(Drupal::requestStack()->getCurrentRequest()->attributes->get('exception'))) {
    $status_code = Drupal::requestStack()->getCurrentRequest()->attributes->get('exception')->getStatusCode();
    $suggestions[] = 'page__' . (string) $status_code;
  }
}
+5

Try the page - system - 404.html.twig

+2
source

Source: https://habr.com/ru/post/1652422/


All Articles