You are here

function user_default_page_user_login in User Default Page 8.2

Same name and namespace in other branches
  1. 8 user_default_page.module \user_default_page_user_login()

Implements hook_user_login().

File

./user_default_page.module, line 81
Contains user_default_page.module..

Code

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);
    }
  }
}