function views_ajax_autocomplete_user

Same name in other branches
  1. 6.x-3.x 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
Implements hook_menu().

File

includes/ajax.inc, line 308

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);
        }
    }
    drupal_json_output($matches);
}