autocomplete field show name pass uid to form In my module I'd like to allow admin/moderator to add some user specific settings e.g. user steve1 has to work between 8am and 3pm.

To make work easier for the admin I'd lke to use autocomplete field for choosing user.

Suggestions are made via user name, but to form, I need to pass uid instead of a name.

Is this possible via modyfinig my autocomplete code, or should I change provided name to uid during form_submit operation?

$matches = array();
$query = db_select('users', 'u');

$return = $query 
  -&gt;fields('u', array(
    'uid',
    'name'
  )) 
  -&gt;condition('u.name', '%' . db_like($string) . '%', 'LIKE')
  -&gt;range(0, 10)
  -&gt;execute();

foreach ($return as $row) {
$matches[$row-&gt;name] = check_plain($row-&gt;name);
}

// return for JS
drupal_json_output($matches);
In your foreach you can add it

foreach ($return as $row) {
   $matches[$row-&gt;name . '[uid:' . $row-&gt;uid . ']'] = check_plain($row-&gt;name);
}


And in your submit handler you can find it with a simple preg_match like this one :

preg_match('/\[uid:(\d+)\]/', $form_state['values']['user_autocomplete'], $matches);
$uid = $matches[1];


Note that the $matches variable are not the same from the auto-complete part and the submit handler part.