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 lipinponmala007 on
I was having the issues all time to create custom autocomplete fields without the id(s) populated in the autocomplete field after selecting. How to do that in my custom form?
Comments
The solution that worked for me is like this
1. add custom_module.js inside the scripts folder
2. in form definition or in form alter make like this (example is for the user id field)
4. In the javascript file put this code
5. In submit function use form_state['values']['user_id_mirror] to get the actual value
Hopes somebody comes with a better approch. and this will be useful to some one.
A small addition...
Yes, I had come to the conclusion that something along these lines had to be done. It mainly works, except that the click on an item in the list of autocompleted items is not captured, and I could not find how to capture it, since it was impossible to analyze the list of autocompleted items with the browsers' developer's tools. So, I had to resort to capturing the click on the document, which isn't very efficient:
var structParse = function() {
var matches = $('#edit-struct-input').val().match(/(.*)(\(:.*\))/) ;
if (matches !== null) {
$('#edit-struct-key').val(matches[2]) ;
$('#edit-struct-input').val(matches[1]) ;
}
} ;
$('#edit-struct-input').bind('keypress keyup change blur click', structParse);
$(document).click(structParse);
Now it does what I want, but having a click event on the whole document is really annoying. I am convinced that textfield with autocomplete suffers from serious design flaws: there was no reason to emulate an HTML SELECT, labels should have been used both in the item list and in the text field, and the value sent in a hidden field.
If somebody could tell me how to capture the click on the item list, or, even better, how to use a custom version of the autocomplete field, it would be very useful!
Subscribe. These autocomplete
Subscribe. These autocomplete fields really suck in D8.
BTW, drupal_add_js() should not be used anymore in D8...
Artatum
www.ixxi-mobility.com