You are here

public function LoginDestinationManager::findDestination in Login Destination 8.2

Same name and namespace in other branches
  1. 8 src/LoginDestinationManager.php \Drupal\login_destination\LoginDestinationManager::findDestination()

Find destination.

Parameters

string $trigger: Trigger.

\Drupal\Core\Session\AccountInterface $account: User account.

Return value

bool|\Drupal\login_destination\Entity\LoginDestination Returns either FALSE or a LoginDestination object.

Overrides LoginDestinationManagerInterface::findDestination

File

src/LoginDestinationManager.php, line 113

Class

LoginDestinationManager
Defines a login destination manager service.

Namespace

Drupal\login_destination

Code

public function findDestination($trigger, AccountInterface $account) {
  $destinations = $this->entityTypeManager
    ->getStorage('login_destination')
    ->loadMultiple();
  uasort($destinations, '\\Drupal\\login_destination\\Entity\\LoginDestination::sort');
  $path = $this
    ->getCurrentPath();
  $path_alias = mb_strtolower($this->aliasManager
    ->getAliasByPath($path));

  // Get user roles.
  $user_roles = $account
    ->getRoles();

  /** @var \Drupal\login_destination\Entity\LoginDestination $destination */
  foreach ($destinations as $destination) {
    if (!$destination
      ->isEnabled()) {
      continue;
    }

    // Determine if the trigger matches that of the login destination rule.
    $destination_triggers = $destination
      ->getTriggers();
    if (!in_array($trigger, $destination_triggers)) {
      continue;
    }
    $destination_roles = $destination
      ->getRoles();
    $role_match = array_intersect($user_roles, $destination_roles);

    // Ensure that the user logging in has a role allowed by the login
    // destination rule and the login destination rule does not have any
    // selected roles.
    if (empty($role_match) && !empty($destination_roles)) {
      continue;
    }
    $destination_language = $destination
      ->getLanguage();
    $lang_code = $this->languageManager
      ->getCurrentLanguage()
      ->getId();
    if ($destination_language != '' && $destination_language != $lang_code) {
      continue;
    }
    $pages = mb_strtolower($destination
      ->getPages());
    if (!empty($pages)) {
      $type = $destination
        ->getPagesType();
      $page_match = $this->pathMatcher
        ->matchPath($path_alias, $pages) || $this->pathMatcher
        ->matchPath($path, $pages);

      // Make sure the page matches(or does not match if the rule specifies
      // that).
      if ($page_match && $type == $destination::REDIRECT_LISTED || !$page_match && $type == $destination::REDIRECT_NOT_LISTED) {
        return $destination;
      }
      continue;
    }
    return $destination;
  }
  return FALSE;
}