user_default_page.module in User Default Page 8.2
Same filename and directory in other branches
Contains user_default_page.module..
File
user_default_page.moduleView source
<?php
/**
* @file
* Contains user_default_page.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Implements hook_help().
*/
function user_default_page_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the user_default_page module.
case 'help.page.user_default_page':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The module allows you to customize the destination that a user is redirected to after logging in or logged out. You can customize by roles or individual users. And customize configurable drupal messages for this actions. Do read README.txt file for more information.') . '</p>';
$output .= '<h3>' . t('Installation') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Enable the module via <a href=":drush_install_module">drush command</a> or the <a href=":extend_link">extend menu</a>.', [
':extend_link' => '/admin/modules',
':drush_install_module' => 'https://www.drupal.org/docs/8/extending-drupal-8/installing-modules-from-the-command-line',
]) . '</dt>';
$output .= '</dl>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('Go to the <a href=":config_page">configuration page</a>.', [
':config_page' => '/admin/config/user_default_page_config_entity/',
]) . '</li>';
$output .= '<li>' . t('Add default pages by clicking on <strong>Add User default page</strong> button or the operation link on each created default page.') . '</li>';
$output .= '<ul>';
$output .= '<li>' . t('Define the page label (just to be easy to identify).') . '</li>';
$output .= '<li>' . t('Select the role, the user or both that will use this default page.') . '</li>';
$output .= '<li>' . t('If you are setting a login page, insert the redirect using internal URL (eg.: /node/5). Also, you can insert a message that will be displayed after the redirect.') . '</li>';
$output .= '<li>' . t('In case of a logout, the configuration is basically the same of the login.') . '</li>';
$output .= '<li>' . t('You can use just login redirect, just logout redirect or both.') . '</li>';
$output .= '</ul>';
$output .= '</li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_user_logout().
*/
function user_default_page_user_logout($account) {
// Load all entities belongs to "user_default_page_config_entity".
$entities_load = \Drupal::entityTypeManager()
->getStorage('user_default_page_config_entity')
->loadMultiple();
// Get User Role(s).
$roles = $account
->getRoles();
$uid = $account
->id();
$logout_redirect = '';
$logout_message = '';
$current_weight = NULL;
// Check for redirect logout path.
foreach ($entities_load as $entity) {
if (array_intersect($entity
->getUserRoles(), $roles)) {
if ($current_weight < $entity
->getWeight()) {
$current_weight = $entity
->getWeight();
$logout_redirect = $entity
->getLogoutRedirect();
}
$logout_message = $entity
->getLogoutRedirectMessage();
}
$users_array = $entity
->getUsers();
if (strpos($users_array, $uid . ',') !== FALSE) {
$logout_redirect = $entity
->getLogoutRedirect();
$logout_message = $entity
->getLogoutRedirectMessage();
}
}
if ($logout_redirect && !empty($logout_message)) {
// Set Logout redirect message.
drupal_set_message(t($logout_message));
user_default_page_redirect($logout_redirect);
}
}
/**
* Implements hook_user_login().
*/
function user_default_page_user_login($account) {
// Load all entities belongs to "user_default_page_config_entity".
$entities_load = \Drupal::entityTypeManager()
->getStorage('user_default_page_config_entity')
->loadMultiple();
// Get User Role(s).
$roles = $account
->getRoles();
$uid = $account
->id();
$login_redirect = '';
$login_message = '';
$current_weight = NULL;
// Check for redirect login path.
foreach ($entities_load as $entity) {
if (array_intersect($entity
->getUserRoles(), $roles)) {
if ($current_weight < $entity
->getWeight()) {
$current_weight = $entity
->getWeight();
$login_redirect = $entity
->getLoginRedirect();
}
$login_message = $entity
->getLoginRedirectMessage();
}
$users_array = $entity
->getUsers();
if (strpos($users_array, $uid . ',') !== FALSE) {
$login_redirect = $entity
->getLoginRedirect();
$login_message = $entity
->getLoginRedirectMessage();
}
}
if ($login_redirect) {
$current_route = \Drupal::service('current_route_match')
->getRouteName();
$ignored_routes = [
'user.reset.login',
'user.reset',
];
\Drupal::moduleHandler()
->alter('user_default_page_login_ignore_whitelist', $ignored_routes);
if (!in_array($current_route, $ignored_routes)) {
// Set Login redirect message.
if ($login_message) {
drupal_set_message(t($login_message));
}
user_default_page_redirect($login_redirect);
}
}
}
/**
* Redirect path for login and logout.
*/
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();
}
Functions
Name | Description |
---|---|
user_default_page_help | Implements hook_help(). |
user_default_page_redirect | Redirect path for login and logout. |
user_default_page_user_login | Implements hook_user_login(). |
user_default_page_user_logout | Implements hook_user_logout(). |