function views_ajax_autocomplete_user in Views (for Drupal 7) 8.3
Same name and namespace in other branches
- 6.3 includes/ajax.inc \views_ajax_autocomplete_user()
- 6.2 includes/ajax.inc \views_ajax_autocomplete_user()
- 7.3 includes/ajax.inc \views_ajax_autocomplete_user()
Page callback for views user autocomplete
Related topics
1 string reference to 'views_ajax_autocomplete_user'
- views_menu in ./
views.module - Implement hook_menu().
File
- includes/
ajax.inc, line 284 - Handles the server side AJAX interactions of Views.
Code
function views_ajax_autocomplete_user($string = '') {
// The user enters a comma-separated list of user name. We only autocomplete the last name.
$array = drupal_explode_tags($string);
// Fetch last name
$last_string = trim(array_pop($array));
$matches = array();
if ($last_string != '') {
$prefix = count($array) ? implode(', ', $array) . ', ' : '';
if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
$matches[$prefix . 'Anonymous'] = 'Anonymous';
}
$result = db_select('users', 'u')
->fields('u', array(
'uid',
'name',
))
->condition('u.name', db_like($last_string) . '%', 'LIKE')
->range(0, 10)
->execute()
->fetchAllKeyed();
foreach ($result as $account) {
$n = $account;
// Commas and quotes in terms are special cases, so encode 'em.
if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
$n = '"' . str_replace('"', '""', $account) . '"';
}
$matches[$prefix . $n] = check_plain($account);
}
}
return new JsonResponse($matches);
}