Symptom
Typing in an autocomplete field intermittently leads to the following fatal JavaScript error:
Uncaught TypeError: suggestions.indexOf is not a function
The error is triggered by showSuggestions() in core/misc/autocomplete.js.
Typing the same input will consistently trigger the error, but there's no obvious pattern for what kind of input triggers it.
Problem
The error is caused by inconsistent JSON structure in AJAX responses. Autocomplete expects an array but is sometimes getting an object with numeric keys.
On the Drupal side, the source of the inconsistency is json_encode(), which is called by Drupal to serialize the JsonResponse for autocomplete. When json_encode() receives an array with sequential keys it serializes as an array. When it receives an array with non-sequential keys it serializes as an object. This behavior was news to me, but it's in the PHP documentation.
ViewsAutocompleteFiltersController::autocomplete() calls array_unique() before returning the JsonResponse, which can *sometimes* lead to non-sequential keys.
Proposed resolution
Always reset array keys before passing autocomplete matches to JsonResponse.
Marking this as major since it compromises the basic functionality of the module.
Comment | File | Size | Author |
---|---|---|---|
#4 | uncaught_typeerror-2824023-4.patch | 824 bytes | wolffereast |
#2 | views-autocomplete-filters-2824023-2-js-error.patch | 542 bytes | milesw |
Comments
Comment #2
milesw CreditAttribution: milesw commentedPatch to reset array keys.
Comment #3
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedShouldn`t we add a comment here? Something like: "Reset array keys to make sure Drupal encodes results as an array"?
Other than that works perfectly :). Thanks a lot!
BTW - not sure if that should be RTBC or Needs work?
Comment #4
wolffereast CreditAttribution: wolffereast commentedPatch from #2 worked for me. Added a comment explaining the change. Leaving in Needs Review so someone can take a look at the comment for validity. If that looks good then I think we are ready to move to RTBC
Comment #6
vasikeThank you all for this.
Solve within related issue #2665388: Duplicate values in dropdown on views with duplicates results
However i applied the array_values to array_unique directly.