Adding multiple users to a field using user/autocomplete I have the following code to add a field to my custom module to add in users. But I can only add in one user using the user/autocomplete feature.

$form['usershare'] = array(   
  '#title' =&gt; t('Share Collection with:'),
  '#type' =&gt; 'textfield',   
  '#autocomplete_path' =&gt; 'user/autocomplete'  
);                   


How do I enable multiple users? These users should be separated by a comma on the back end for me to process it.
as @Geat said:

Your form should be like below.

    $form['usershare'] = array(   
      '#title' =&gt; t('Share Collection with:'),
      '#type' =&gt; 'textfield',   
      '#autocomplete_path' =&gt; 'user/autocomplete',
      '#multiple' =&gt; TRUE,
    );

    // Implemenbts hook_menu() for user/autocomplete path.
    function module_name_menu() {  
      $items['user/autocomplete'] = array(
        'page callback' =&gt; '_user_autocomplete',
        'access arguments' =&gt; array('access user autocomplete'),
        'type' =&gt; MENU_CALLBACK
      );
      return $items;
    }

    // Implements callback function to get users in autocomplete with comma seperated
    function _user_autocomplete($string) {
      if ($string) {
        $items = array_map('trim', explode(',', $string));
        $last_item = array_pop($items);
        $prefix = implode(', ', $items);
        $result = db_select('users')-&gt;fields('users', array('name'))-&gt;condition('name', '%' . db_like($last_item) . '%', 'LIKE')-&gt;range(0, 10)-&gt;execute();
        foreach ($result as $user) {
          $value = !empty($prefix) ? $prefix . ', ' . $user-&gt;name : $user-&gt;name;
          $matches[$value] = check_plain($value);
        }
      }
    // return output in JSON format.
    drupal_json_output($matches);
}