You are here

customerror.module in Customerror 8

Same filename and directory in other branches
  1. 5 customerror.module
  2. 6 customerror.module
  3. 7 customerror.module

Enables custom 404 (not found) and 403 (access denied) pages in Drupal with no need for creating real nodes under taxonomies.

File

customerror.module
View source
<?php

/**
 * @file
 * Enables custom 404 (not found) and 403 (access denied) pages in Drupal
 * with no need for creating real nodes under taxonomies.
 */
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function customerror_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/help#customerror':
    case 'admin/config/system/customerror':
      $output = t('Enables the creation of custom error pages for 404 and 403 errors.');
      break;
  }
  return $output;
}

/**
* Implements hook_user_login().
function customerror_user_login($account) {
 // Check if we have a destination saved in the session.
 if (!empty($_SESSION['destination'])) {
   // If there is one, then set the REQUEST destination to it.
   $dest = UrlHelper::parse($_SESSION['destination']);
   if (isset($dest['query']['destination'])) {
     $dnode = $dest['query']['destination'];
   }
   elseif (isset($dest['path'])) {
     $dnode = $dest['path'];
   }
   else {
     $dnode = \Drupal::config('system.site')->get('page.front');
   }
   $_GET['destination'] = $dnode;
   // And clear the one in the session.
   unset($_SESSION['destination']);
   // user.module then does a drupal_goto() for us after we return from here.
 }
}
*/

/**
 * Sets the http header depending on the error code (403 or 404).
 *
 * @param int $code
 *   The error code, either 403 or 404.
 */
function customerror_header($code) {
  switch ($code) {
    case 403:
      header("HTTP/1.0 403 Access Denied");
      break;
    case 404:
      header("HTTP/1.0 404 Not Found");
      break;
  }
}

/**
 * Gets the configured display theme for the given error code.
 *
 * @param int $code
 *   The code of error.
 *
 * @return sting $theme
 *   The name of theme.
 */
function customerror_get_theme($code = 404) {
  $theme_default = \Drupal::config('system.theme')
    ->get('admin');
  $customerror_theme = \Drupal::config('customerror.config')
    ->get("customerror_{$code}.theme");
  $theme = !empty($customerror_theme) ? $customerror_theme : $theme_default;
  return $theme;
}

/**
 * Implements hook_theme() to add the template definition.
 */
function customerror_theme($existing, $type, $theme, $path) {
  return [
    'customerror' => [
      'variables' => [
        'description' => NULL,
        'login_form' => NULL,
      ],
    ],
  ];
}

/**
* Implements hook_form_FORM_ID_alter().
function customerror_form_user_login_form_alter(&$form, FormStateInterface $form_state) {
 $form['#submit'][] = 'customerror_user_login_submit';
}
*/

/**
 * Form submission handler for user_login_form().
 *
 * Redirects the user to the destination we set in after logging in.
 * Feels like this shouldn't be necessary somehow, right?  Weird
 */
function customerror_user_login_submit(&$form, FormStateInterface $form_state) {

  // Check if a destination was set, probably on an exception controller.
  // @see \Drupal\user\Form\UserLoginForm::submitForm()
  $request = \Drupal::service('request_stack')
    ->getCurrentRequest();
  if (!$request->request
    ->has('destination')) {
    $form_state
      ->setRedirectUrl($url);
  }
  else {
    $request->query
      ->set('destination', $request->request
      ->get('destination'));
  }
}

Functions

Namesort descending Description
customerror_get_theme Gets the configured display theme for the given error code.
customerror_header Sets the http header depending on the error code (403 or 404).
customerror_help Implements hook_help().
customerror_theme Implements hook_theme() to add the template definition.
customerror_user_login_submit Form submission handler for user_login_form().