Problem/Motivation
There is no way to add a value to the field from JavaScript. A list of terms drawn beside the field, where clicking one should add it, has nothing to call: the widget is built by the behaviour and the instance is thrown away.
Steps to reproduce
- Open a page with an Autocomplete Deluxe field.
- Look for a handle on the widget from the console.
Proposed resolution
Keep the widget in the jQuery data of the input, so addValue can be called:
jQuery('#autocomplete-deluxe-input').data('autocompleteDeluxe').addValue({ label: 'Drupal', value: 'Drupal (7)' });
The value is the same Label (id) pair the field stores, so the tag, the hidden value and the saved reference line up.

Remaining tasks
- ✅ File an issue
- ✅ Addition/Change/Update/Fix
- ✅ Testing to ensure no regression
- ➖ Automated unit testing coverage
- ➖ Automated functional testing coverage
- ➖ UX/UI designer responsibilities
- ➖ Readability
- ➖ Accessibility
- ➖ Performance
- ➖ Security
- ➖ Developer Documentation
- ➖ User Guide Documentation
- ➖ Reviewed by human
- ➖ Code review by maintainers
- ➖ Full testing and approval
- ➖ Credit contributors
- ➖ Review with the product owner
- ➖ Release notes snippet
- ❌ Release
User interface changes
- N/A
API changes
- The widget instance is available as jQuery data on the input, under autocompleteDeluxe.
Data model changes
- N/A
Release notes snippet
- The widget instance is available as jQuery data on the input, under autocompleteDeluxe.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 3061562-add-from-javascript.png | 59.95 KB | rajab natshah |
| #7 | adds-widget-object-reference-to-element-3061562-1.patch | 529 bytes | eadamse |
| #6 | 0001-Adds-widget-object-reference-to-element.patch | 1.14 KB | mtovey |
Issue fork autocomplete_deluxe-3061562
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
sic commenteda possible way could maybe be something like this:
Comment #3
daveianoI have a similar use case - I also need to set the values of the automcomplete_deluxe field programmatically via JS.
Any luck with that? The code you posted is the initialization of the JS field widget from the module?
Comment #4
sic commented@daveiano i changed the module as mentioned above adding $(this).data('autocomplete_deluxe', widget); ...
Comment #5
rajab natshahComment #6
mtovey commentedAttaching a patch that changes the module js as @sic outlined earlier. This then allows subsequent js code with a reference to the HTML widget element to access the properties/methods from the MultipleWidget setup method. For example:
let widgetObject = jQuery('#autocomplete-deluxe-input').data('autocomplete_deluxe_widget');
let itemInit = {
label: 'Create a new tag: #Sample tag',
value: 'Sample tag',
newTerm: true
};
widgetObject.addValue(itemInit);
Comment #7
eadamse commentedThis is a re-roll of the patch from #6 for version ^2.1. You can utilize the exposed widget object using the jQuery `data` method as described there.
Comment #9
rajab natshahThank you, Sic for asking and David and Michael for keeping the thread alive.
There was nothing to call, because the behaviour created the widget and dropped the instance. The merge request keeps it in the jQuery data of the input, so this works:
jQuery('#autocomplete-deluxe-input').data('autocompleteDeluxe').addValue({ label: 'Drupal', value: 'Drupal (7)' });
Tested on Drupal 11.4: two values added that way show as tags, the hidden field reads the Label (id) pairs, and saving the node stores term IDs 7 and 9.
To review, test, then merge.
Comment #11
rajab natshahComment #13
rajab natshah