You are here

protected function LoginRedirectPerRole::getRedirectUrl in Login And Logout Redirect Per Role 8

Return redirect URL related to requested key and current user.

Parameters

string $key: Configuration key (login or logout).

\Drupal\Core\Session\AccountInterface|null $account: User account to get redirect URL for.

Return value

\Drupal\Core\Url|null Redirect URL related to requested key and current user.

3 calls to LoginRedirectPerRole::getRedirectUrl()
LoginRedirectPerRole::getLoginRedirectUrl in src/LoginRedirectPerRole.php
Return URL to redirect on user login.
LoginRedirectPerRole::getLogoutRedirectUrl in src/LoginRedirectPerRole.php
Return URL to redirect on user logout.
LoginRedirectPerRole::setDestination in src/LoginRedirectPerRole.php
Set "destination" parameter to do redirect.

File

src/LoginRedirectPerRole.php, line 161

Class

LoginRedirectPerRole
Login And Logout Redirect Per Role helper service.

Namespace

Drupal\login_redirect_per_role

Code

protected function getRedirectUrl($key, AccountInterface $account = NULL) {
  $url = NULL;
  switch ($key) {
    case LoginRedirectPerRoleInterface::CONFIG_KEY_LOGIN:
      if (!$this
        ->isApplicableOnCurrentPage()) {
        return $url;
      }
      break;
  }
  $config = $this
    ->getConfig($key);
  if (!$config) {
    return $url;
  }
  $user_roles = $this
    ->getUserRoles($account);
  $destination = $this->currentRequest->query
    ->get('destination');
  foreach ($config as $role_id => $settings) {

    // Do action only if user have a role and
    // "Redirect URL" is set for this role.
    if (in_array($role_id, $user_roles) && $settings['redirect_url']) {

      // Prevent redirect if destination usage is allowed.
      if ($settings['allow_destination'] && $destination) {
        break;
      }
      if ($settings['redirect_url'] === '<front>') {
        $url = Url::fromRoute($settings['redirect_url']);
        break;
      }
      $path = $this->token
        ->replace($settings['redirect_url']);
      $url = Url::fromUserInput($this
        ->stripSubdirectoryFromPath($path));
      break;
    }
  }
  return $url;
}