You are here

function login_destination_toolbar_alter in Login Destination 8.2

Same name and namespace in other branches
  1. 8 login_destination.module \login_destination_toolbar_alter()

Implements hook_toolbar_alter().

File

./login_destination.module, line 103
Control where users are directed to, once they login, register or logout.

Code

function login_destination_toolbar_alter(&$items) {
  if (empty($items['user']['tray']['user_links'])) {
    return;
  }

  // Disable cache for user links in toolbar, to be able set current param.
  $items['user']['tray']['user_links']['#cache']['context'] = [];
  $items['user']['tray']['user_links']['#cache']['max-age'] = 0;
  if (\Drupal::currentUser()
    ->isAnonymous()) {
    $url =& $items['user']['tray']['user_links']['#links']['login']['url'];

    // Change route name, since route "user.page" always redirects to
    // "user.login".
    $url = Url::fromRoute('user.login');

    // Get current path.
    $current = \Drupal::service('path.current')
      ->getPath();

    // Add current param to be able to evaluate previous page.
    $url
      ->setOptions([
      'query' => [
        'current' => $current,
      ],
    ]);
  }
}