I have created custom module to add user awards. This award = some text(like Php developer of the month) and image, and i need to implement feature, which can add this award to multiple users in this form This is my form:

function user_awards_admin_settings() {
    $form = array();
    $form['award_description'] = array(
        '#type' => 'textfield',
        '#title' => 'award description',

    $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Assign to'),
        '#autocomplete_path' => 'user_awards/autocomplete',
    $form['user_awards_block_image'] = array(
        '#type' => 'managed_file',
        '#name' => 'user_awards_block_image',
        '#title' => t('Block image'),
        '#size' => 40,
        '#description' => t("Image should be less than 400 pixels wide"),
        '#upload_location' => 'public://'

    $form['#submit'][] = 'user_awards_settings_submit';
    return system_settings_form($form);

And this is my autocomplete function:

function user_awards_autocomplete($string) {
    $matches = array();
    $return = db_query("SELECT name FROM users where lower(name) like '" . $string . "%' LIMIT 10");
    // add matches to $matches
    foreach ($return as $row) {
        $matches[$row->name] = check_plain($row->name);
    // return for JS

And submit one:

function user_awards_settings_submit($form, $form_state) {
    $file = '';
    if (isset($form_state['values']['user_awards_block_image'])) {
        $file = file_load($form_state['values']['user_awards_block_image']);

        $file->status = FILE_STATUS_PERMANENT;

    $description = $form_state['input']['award_description'];
    $name = $form_state['input']['name'];
    db_query('INSERT INTO {user_awards} (name, award_description, fid) VALUES (:name, :award_description, :fid)', array(
        ':name' => $name,
        ':award_description' => $description,
        ':fid' => $file->fid,
    drupal_set_message('form has been submitted');


I need somehow to make $form['name'] to be multiple(autocomplete) textfield, comma separated or so, how can i do it? At this moment its only autocomplete first value of NAME field

2 Answers 2


Add a select list to your form. Add some #ajax to your textfield. Every time the textfield changes content, add the value (via #ajax) to your select list. Use the values of your select list in the submit callback.


You could to it by attaching a custom JavaScript file using $form['#attached']['js'] with some good old JavaScript to fetch the users and add them to the csv textfield (autocomplete). Then split the values in the submit and add them to the database.


Just use a select if that's OK with you.


try next code to autocomplete callback (d6 implementation from my custom module)

function _nodetype_autocomplete_multiple($string) {
  // The user enters a comma-separated list of content types. We only autocomplete the last content type.
  $array = drupal_explode_tags($string);
  // Fetch last
  $last_string = trim(array_pop($array));
  $matches = array();
  $result = db_query_range("SELECT type FROM {node_type} WHERE LOWER(type) LIKE LOWER('%s%%')", $last_string, 0, 10);
  $prefix = count($array) ? implode(', ', $array) .', ' : '';
  while ($type = db_fetch_object($result)) {
    $matches[$prefix . $type->type] = check_plain($type->type);


you may want to see full code at github

  • This does not work in Drupal 7 since they removed db_fetch_object(). So sad. :(
    – user785179
    Nov 21, 2012 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.