public function UserAutocomplete::getMatches

Get matches for the autocompletion of user names.

Parameters

string $string: The string to match for usernames.

bool $include_anonymous: (optional) TRUE if the the name used to indicate anonymous users (e.g. "Anonymous") should be autocompleted. Defaults to FALSE.

Return value

array An array containing the matching usernames.

File

drupal/core/modules/user/lib/Drupal/user/UserAutocomplete.php, line 58
Contains \Drupal\user\UserAutocomplete.

Class

UserAutocomplete
Defines a helper class to get user autocompletion results.

Namespace

Drupal\user

Code

public function getMatches($string, $include_anonymous = FALSE) {
  $matches = array();
  if ($string) {
    if ($include_anonymous) {
      $anonymous_name = $this->configFactory
        ->get('user.settings')
        ->get('anonymous');

      // Allow autocompletion for the anonymous user.
      if (stripos($anonymous_name, $string) !== FALSE) {
        $matches[$anonymous_name] = check_plain($anonymous_name);
      }
    }
    $result = $this->connection
      ->select('users')
      ->fields('users', array(
      'name',
    ))
      ->condition('name', db_like($string) . '%', 'LIKE')
      ->range(0, 10)
      ->execute();
    foreach ($result as $account) {
      $matches[$account->name] = check_plain($account->name);
    }
  }
  return $matches;
}