javali_error_pages-1.0.5/javali_error_pages.module

javali_error_pages.module
<?php

/**
 * @file
 * Module file for javali_error_pages.
 */

use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function javali_error_pages_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the javali_error_pages module.
    case 'help.page.javali_error_pages':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module adds a custom 403/404/maintenance page.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function javali_error_pages_theme() {
  return [
    'page__system__403' => [
      'variables' => [
        'items' => NULL,
      ],
    ],
    'page__system__404' => [
      'variables' => [
        'items' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function javali_error_pages_preprocess_page__system__403(&$variables) {
  $variables['logo'] = _javali_error_pages_get_logo();
  $variables['image_403'] = '/' . \Drupal::service('extension.list.module')->getPath('javali_error_pages') . '/css/images/403.png';
  $variables["#attached"]["library"][] = 'javali_error_pages/error_pages';

  $theme_name = \Drupal::theme()->getActiveTheme()->getName();
  $variables['page']['content'] = $variables['page']['content'][$theme_name . '_content'];
  $variables['front_page'] = Url::fromRoute('<front>')->toString();
}

/**
 * Implements hook_preprocess_HOOK().
 */
function javali_error_pages_preprocess_page__system__404(&$variables) {
  $variables['logo'] = _javali_error_pages_get_logo();
  $variables['image_404'] = '/' . \Drupal::service('extension.list.module')->getPath('javali_error_pages') . '/css/images/404.png';
  $variables["#attached"]["library"][] = 'javali_error_pages/error_pages';

  $theme_name = \Drupal::theme()->getActiveTheme()->getName();
  $variables['page']['content'] = $variables['page']['content'][$theme_name . '_content'];
  $variables['front_page'] = Url::fromRoute('<front>')->toString();
}

/**
 * Implements hook_preprocess_HOOK().
 */
function javali_error_pages_preprocess_maintenance_page(&$variables) {
  $variables['logo'] = _javali_error_pages_get_logo();
  $variables['maintenance'] = '/' . \Drupal::service('extension.list.module')->getPath('javali_error_pages') . '/css/images/maintenance.png';
  $variables["#attached"]["library"][] = 'javali_error_pages/error_pages';
  $variables['front_page'] = Url::fromRoute('<front>')->toString();
}

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function javali_error_pages_theme_suggestions_page_alter(&$suggestions, $variables, $hook) {

  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__system__' . (string) $status_code;
        break;

      case 403:
        $suggestions[] = 'page__system__' . (string) $status_code;
        break;

      default:
        break;
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function javali_error_pages_theme_registry_alter(&$theme_registry) {
  $theme_registry['maintenance_page']['path'] = \Drupal::service('extension.list.module')->getPath('javali_error_pages') . '/templates';
}

/**
 * Gets the page logo.
 */
function _javali_error_pages_get_logo() {
  $theme_name = \Drupal::theme()->getActiveTheme()->getName();
  $logo = \Drupal::config($theme_name . '.settings')->get('logo.path');
  if (empty($logo)) {
    $logo = '/' . \Drupal::service('extension.list.module')->getPath('javali_error_pages') . '/css/images/logo_drupal.svg';
  }
  else {
    $logo = \Drupal::service('file_url_generator')->generateAbsoluteString($logo);
  }

  return $logo;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc