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.
Comment | File | Size | Author |
---|---|---|---|
autocomplete-nid-vid-extraction-D7.patch | 2.73 KB | cdale | |
autocomplete-nid-vid-extraction-D6.patch | 2.73 KB | cdale | |
Comments
Comment #1
danielb CreditAttribution: danielb commentedI 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.
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedThe last submitted patch failed testing.
Comment #3
bisbell CreditAttribution: bisbell commentedI 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.
Comment #4
frdesign CreditAttribution: frdesign commentedI 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:
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?:
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?
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.
I'd really appreciate it if you could shed some light into what I may be doing wrong.
Comment #5
chuckdeal97 CreditAttribution: chuckdeal97 commentedSee #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.
Comment #6
catchMoving to Drupal 8.
Comment #7
nonsiesubscribe
Comment #9
paco6659 CreditAttribution: paco6659 commentedautocomplete-nid-vid-extraction-D7.patch queued for re-testing.
Comment #11
AaronBaumansubscribe
Comment #12
danielb CreditAttribution: danielb commentedOn 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
Comment #16
kattekrab CreditAttribution: kattekrab at Catalyst IT for La Trobe University commentedComment #17
kattekrab CreditAttribution: kattekrab as a volunteer commentedAdding related issue mentioned in comments
#365241: Add select event to autocomplete feature