Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Floris246 on
Hi there!
I've a Drupal textfield with a #autocomplete_path attribute. When the user add's a value to that field I would like to change the autocomplete path. I would like to add the value of an other field to the Ajax call that will be made. For example: album/autocomplete/work_by_composer/input-of-this-field/input-of-previous-field
I tried this with jQuery without success:
if (Drupal.jsEnabled) {
$(document).ready(function() {
$("#edit-composer").change(function() {
var composer = $("#edit-composer").val();
$('#edit-work-autocomplete').val("http://example.com/album/autocomplete/work_by_composer/" + composer + "/");
})
})
}
Can somebody help me with this?
Comments
The solution!
My colleague just found a solution:
The class "autocomplete-processed" has to be removed and the autocomplete field has to be reset as well.
Makes calls to the previous uri too
Found the same solution. It works but it still makes calls to the previous uri. Thus, e.g. you had 'path/autocomplete/1' that returned
abd abde
for 'ab' and you dynamically change it to 'path/autocomplete/2' that returnsabcd abcde
for 'ab'. In that case you will still getabd abde
for 'ab'. That's because the old event is fired first.To escape this you need to unbind the event from the textfield (not hidden value). In your case it is
$('#edit-work').unbind().val('');
Here we also clear preselected value.For more detail see, for ex., location_autocomplete.js (http://bak.ndla.no/cgi-bin/trac.cgi/browser/modules/3rdparty/drupal/loca...)
Tnx to http://stackoverflow.com/users/9143/john-fiala
I've tried using this
I've tried using this approach to change my autocomplete path, but I'm getting this error constantly:
Also, Drupal.jsEnabled has been deprecated, what version was your solution for?Drupal 7 method
I was attempting this in Drupal 7, and had the same issues, succinct, so ended up trying a different approach.
Instead of trying to change the autocomplete url, you can override drupal's autocomplete behaviour and do things differently.
This blog post explains how to do it for Drupal 6, but the approach is the same, with slightly different code (copied from autocomplete.js): http://sachachua.com/blog/2011/08/drupal-overriding-drupal-autocompletio...
If anyone knows a better approach, please post back.
drupal 7 code
I'm using this successfully on a Drupal 7 site (well, it's edited to make sense out of context)
I was caught out at first by spelling behaviours the English way, with a 'U' :-)
thanks
thanks to Joe for posting his piece.
$('#hidden-field-holding-the-url')
.removeClass('autocomplete-processed')
.val('/new/path');
$('#text-field-with-autocomplete')
.unbind();
Drupal.behaviors.autocomplete.attach(document);
--
I was banging my head against this trying to get it to work and that last piece was the key.