Skip to content
Snippets Groups Projects
Commit 1c06ef40 authored by Angie Byron's avatar Angie Byron
Browse files

Issue #1892836 by dawehner, alexpott: Use user/autocomplete instead of views...

Issue #1892836 by dawehner, alexpott: Use user/autocomplete instead of views own autocomplete function.
parent 1f994597
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -45,7 +45,7 @@ function value_form(&$form, &$form_state) {
'#title' => t('Usernames'),
'#description' => t('Enter a comma separated list of user names.'),
'#default_value' => $default_value,
'#autocomplete_path' => 'admin/views/ajax/autocomplete/user',
'#autocomplete_path' => 'user/autocomplete/anonymous',
);
if (!empty($form_state['exposed']) && !isset($form_state['input'][$this->options['expose']['identifier']])) {
......
......@@ -176,26 +176,4 @@ public function testExposedFilter() {
}
}
/**
* Tests the autocomplete function.
*
* @see views_ajax_autocomplete_user
*/
public function testUserAutocomplete() {
module_load_include('inc', 'views', 'includes/ajax');
// Nothing should return no user.
$result = (array) json_decode(views_ajax_autocomplete_user(''));
$this->assertFalse($result);
// A random user should also not be findable.
$result = (array) json_decode(views_ajax_autocomplete_user($this->randomName())->getContent());
$this->assertFalse($result);
// A valid user should be found.
$result = (array) json_decode(views_ajax_autocomplete_user($this->names[0])->getContent());
$expected_result = array($this->names[0] => $this->names[0]);
$this->assertIdentical($result, $expected_result);
}
}
......@@ -278,53 +278,6 @@ function views_ajax_form_wrapper($form_id, &$form_state) {
return $output;
}
/**
* Page callback for views user autocomplete.
*
* @param string $string
* (optional) A comma-separated list of user names entered in the
* autocomplete form element. If not passed, it is taken from the 'q' query
* string parameter.
*
* @return Symfony\Component\HttpFoundation\JsonResponse
*/
function views_ajax_autocomplete_user($string = NULL) {
if (!isset($string)) {
$string = drupal_container()->get('request')->query->get('q');
}
// 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);
}
/**
* Page callback for views taxonomy autocomplete.
*
......
......@@ -304,16 +304,6 @@ function views_menu() {
'type' => MENU_CALLBACK,
'file' => 'includes/ajax.inc',
);
// Path is not admin/structure/views due to menu complications with the wildcards from
// the generic ajax callback.
$items['admin/views/ajax/autocomplete/user'] = array(
'page callback' => 'views_ajax_autocomplete_user',
'theme callback' => 'ajax_base_page_theme',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'includes/ajax.inc',
);
// Define another taxonomy autocomplete because the default one of drupal
// does not support a vid a argument anymore
$items['admin/views/ajax/autocomplete/taxonomy/%'] = array(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment