public function LoginDestinationManager::findDestination in Login Destination 8
Same name and namespace in other branches
- 8.2 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
Overrides LoginDestinationManagerInterface::findDestination
File
- src/
LoginDestinationManager.php, line 92
Class
- LoginDestinationManager
- Defines a login destination manager service.
Namespace
Drupal\login_destinationCode
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 = Unicode::strtolower($this->aliasManager
->getAliasByPath($path));
// Get user roles.
$user_roles = $account
->getRoles();
/** @var 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 the user logging in has a role allowed by the login destination rule.
// Or Login Destination Rule does not have any selected roles.
if (empty($role_match) && !empty($destination_roles)) {
continue;
}
$destination_language = $destination
->getLanguage();
$lang_code = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if ($destination_language != '' && $destination_language != $lang_code) {
continue;
}
$pages = Unicode::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;
}