At the moment, autocomplete textfields do not allow for any kind of unique identification of which value has been chosen without putting id's in the textfield itself. This patch aims to change this so the ids can be placed in a hidden field.

With this patch, you could create a form such as:

$form['search']['name'] = array(
  '#type' => 'textfield',
  '#autocomplete_path' => 'path/to/autocomplete/callback',
  '#default_value' => '',
);
$form['search']['name_nid'] = array(
  '#type' => 'hidden',
  '#default_value' => '',
);
$form['search']['name_vid'] = array(
  '#type' => 'hidden',
  '#default_value' => '',
);

With the autocomplete function using something like this:

  // Code to get results
  foreach ($results) {
    $matches[$result->title .' [nid:'. $result->nid .'] [vid:'. $result->vid .']'] = $result->title;
  }
  drupal_json($matches);

Pretty standard so far. To utilize this patch, you need to add the following when creating your autocomplete form.

  drupal_add_js(array('autocomplete_ids' => array('edit-search-name' => 'Drupal.jsAC.prototype.extractVid')), "setting");
  drupal_add_js(array('autocomplete_ids' => array('edit-search-name' => 'Drupal.jsAC.prototype.extractNid')), "setting");

You could also specify it in one line like this:

  drupal_add_js(array('autocomplete_ids' => array('edit-search-name' => array('Drupal.jsAC.prototype.extractVid', 'Drupal.jsAC.prototype.extractNid'))), "setting");

Where 'edit-search-name' is the id of the autocomplete field. The 'extraction' functions will then be called in the order supplied when a value is chosen and return a value minus the extracted text. The extracted ids are then placed in 'edit-search-name-nid' and 'edit-search-name-vid', ready to be submitted.

This also allows for custom 'extraction' functions to be written. The Drupal.jsAC.prototype.extractNid which is included in the patch is shown below. the extractVid one is mostly the same, except it uses vid not nid.

Drupal.jsAC.prototype.extractNid = function(value, id) {
  var matches = /^(.*) \[nid:([0-9]{1,10})\]$/.exec(value);

  if (matches[1] && matches[2]) {
    $('#' + id + '-nid').val(matches[2]);
    return matches[1];
  }
  else {
    return '';
  }
};

I was messing around with this idea, and thought others might be interested. It shouldn't break any existing functionality.

Do people think this would be useful? If the current functionality is fine for your needs, then you don't need to do anything, but I think this adds a very useful feature.

7.x and 6.x patches attached.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

danielb’s picture

I would be very interested in this as I am worried the [nid: 34] style appendage might be unfriendly for some users, and it certainly seems like we need a different solution.

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

bisbell’s picture

I agree w/danielb. I'm still going to use it and see if it effects adoption of the tool I'm building, but I'd be really happy to see this changed.

frdesign’s picture

I think this is an awesome idea, unfortunately I haven't been able to make it work in my custom module. My autocomplete form returns names of cities from the db and I need to get the city ID into a hidden form field. I applied the patch and tried to follow along the rest of the instructions but I must be doing something wrong. Everything works fine except that my hidden field remains empty after I select a city from the pop up choices and submit.

These are my form items including the hidden one where I need the ID # to go into:

<?php
            $form['globalize']['city'] = array(
                '#type' => 'textfield',
                '#title' => t('City'),
                '#size' => 40,
                '#default_value' => '',
                '#autocomplete_path' => 'city/autocomplete',
            );
            
            $form['globalize']['cityID'] = array(
                '#type' => 'hidden',
                '#default_value' => '',
            );
            
            $form['globalize']['save'] = array(
              '#type' => 'submit', 
              '#value' => t('Save'),
            );
?>

Here's my autocomplete function which puts the cityID into the text field. Does the ID have to be shown to the user like this?:

<?php
  if ($string) {
    $result = db_query_range("SELECT c.cityName, c.cityID, r.regionName FROM {globalize_cities} c INNER JOIN {globalize_regions} r ON c.regionID = r.regionID AND c.countryID = r.countryID WHERE c.countryID = '%s' AND LOWER(cityName) LIKE LOWER('%s%%')", $countryID, $string, 0, 10);
    while ($city = db_fetch_object($result)) {
      // Concatenates region name to city name for clarity
      $matches[$city->cityName . ' (' . $city->regionName . $region_label . ')' . '[cityID: ' . $city->cityID . ']' ] = $city->cityName. ' (' . $city->regionName . $region_label . ')';
    }
  }
?>

Here's the code that's supposed to be included as you build the form. I put it right before my autocomplete form item since I'm not sure where exactly this is supposed to go. Is that the wrong location?

<?php
drupal_add_js(array('autocomplete_ids' => array('edit-globalize-city' => 'Drupal.jsAC.prototype.extractCityid')), "setting");
?>

And here's the modified helper function. I commented out the second one for vid that was included in the patch since I don't need it.

/**
 * Helper function to extract a cityID
 */
Drupal.jsAC.prototype.extractCityid = function(value, id) {
  var matches = /^(.*) \[cityID:([0-9]{1,10})\]$/.exec(value);

  if (matches[1] && matches[2]) {
    $('#' + id + '-cityID').val(matches[2]);
    return matches[1];
  }
  else {
    return '';
  }
};

I'd really appreciate it if you could shed some light into what I may be doing wrong.

chuckdeal97’s picture

See #365241: Add select event to autocomplete feature, where I posted a patch that allows this to be a pure js solution with only a small change to the autocomplete.js. This is in the same vein as your idea, but with less intrusion into autocomplete.js.

catch’s picture

Version: 7.x-dev » 8.x-dev

Moving to Drupal 8.

nonsie’s picture

subscribe

paco6659’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, autocomplete-nid-vid-extraction-D7.patch, failed testing.

AaronBauman’s picture

subscribe

danielb’s picture

On this topic I notice somebody has created a sandbox module widget thing that is designed to replace an autocomplete textfield, and gives the 'keys' in the same fashion that a select/checkboxes/radios does.
http://drupal.org/sandbox/keithm/1216204

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kattekrab’s picture

kattekrab’s picture

Adding related issue mentioned in comments
#365241: Add select event to autocomplete feature

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.