Get matches for the autocompletion of user names.
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.
array An array containing the matching usernames.
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;
}