You are here

function user_default_page_redirect in User Default Page 8

Same name and namespace in other branches
  1. 8.2 user_default_page.module \user_default_page_redirect()

Redirect path for login and logout.

2 calls to user_default_page_redirect()
user_default_page_user_login in ./user_default_page.module
Implements hook_user_login().
user_default_page_user_logout in ./user_default_page.module
Implements hook_user_logout().

File

./user_default_page.module, line 122
Contains user_default_page.module..

Code

function user_default_page_redirect($path) {
  if (!preg_match('/^http/', $path) && !preg_match('/^node/', $path)) {
    $http = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https://" : "http://";
    $http_host = $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
    $path_url = str_replace('//', '/', $http_host . $path);
    $path = $http . $path_url;
  }

  // Check if rename_admin_paths module enable.
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('rename_admin_paths')) {
    $config = \Drupal::config('rename_admin_paths.settings');
    if ($config
      ->get('admin_path')) {
      $admin_path_value = $config
        ->get('admin_path_value');
      if (!strpos($path, '/admin/') === FALSE) {
        $path = preg_replace("#/admin/#", "/{$admin_path_value}/", $path);
      }
      elseif (!strpos($path, '/admin') === FALSE) {
        $path = preg_replace("#\\/admin\$#", "/{$admin_path_value}", $path);
      }
    }
    if ($config
      ->get('user_path')) {
      $user_path_value = $config
        ->get('user_path_value');
      if (!strpos($path, '/user/') === FALSE) {
        $path = preg_replace("#/user/#", "/{$user_path_value}/", $path);
      }
      elseif (!strpos($path, '/user') === FALSE) {
        $path = preg_replace("#\\/user#", "/{$user_path_value}", $path);
      }
    }
  }
  $url_object = \Drupal::service('path.validator')
    ->getUrlIfValid($path);
  $url = $url_object
    ->toString();
  $response = new RedirectResponse($url);
  $response
    ->send();
}