You are here

public function UserEmailVerification::getUserByNameOrEmail in User email verification 8

Load Drupal user by name or email.

Parameters

string $name_or_email: User name/login or email.

bool $active_only: Whether to search for active users only.

Return value

null|\Drupal\user\UserInterface Drupal user on success or NULL otherwise.

Overrides UserEmailVerificationInterface::getUserByNameOrEmail

File

src/UserEmailVerification.php, line 571

Class

UserEmailVerification
User email verification helper service.

Namespace

Drupal\user_email_verification

Code

public function getUserByNameOrEmail($name_or_email, $active_only = TRUE) {
  if (!$name_or_email) {
    return NULL;
  }
  $user_storage = $this->entityTypeManager
    ->getStorage('user');
  $query = $user_storage
    ->getQuery();
  $name_email_condition = $query
    ->orConditionGroup()
    ->condition('name', $name_or_email)
    ->condition('mail', $name_or_email);
  $query
    ->condition($name_email_condition);
  if ($active_only) {
    $query
      ->condition('status', 1);
  }
  $uids = $query
    ->execute();
  $uid = reset($uids);
  return $uid ? $user_storage
    ->load($uid) : NULL;
}