1 ajax_example_autocomplete.inc ajax_example_simple_user_autocomplete_callback($string = "")

This is just a copy of user_autocomplete().

It works simply by searching usernames (and of course in Backdrop usernames are unique, so can be used for identifying a record.)

The returned $matches array has

  • key: string which will be displayed once the autocomplete is selected
  • value: the value which will is displayed in the autocomplete pull-down.

In the simplest cases (see user_autocomplete()) these are the same, and nothing needs to be done. However, more more complicated autocompletes require more work. Here we demonstrate the difference by displaying the UID along with the username in the dropdown.

In the end, though, we'll be doing something with the value that ends up in the textfield, so it needs to uniquely identify the record we want to access. This is demonstrated in ajax_example_unique_autocomplete().

Parameters

string $string: The string that will be searched.

File

modules/examples/ajax_example/ajax_example_autocomplete.inc, line 67
ajax_example_autocomplete.inc

Code

function ajax_example_simple_user_autocomplete_callback($string = "") {
  $matches = array();
  if ($string) {
    $result = db_select('users')
      ->fields('users', array('name', 'uid'))
      ->condition('name', db_like($string) . '%', 'LIKE')
      ->range(0, 10)
      ->execute();
    foreach ($result as $user) {
      // In the simplest case (see user_autocomplete), the key and the value are
      // the same. Here we'll display the uid along with the username in the
      // dropdown.
      $matches[$user->name] = check_plain($user->name) . " (uid=$user->uid)";
    }
  }

  backdrop_json_output($matches);
}