I've done the cursory googling, but so far I haven't found anything close to an answer for this.

I'm using the Masquerade module with a custom module on top to extend it and make its searching algorithms a bit more "clever" (Soundexes, custom fields and such), and it returns the results in a certain order, passed through to drupal_json_output(). However, because JSON isn't exactly indexed, it means that when the autocomplete text field picks it up, it's in a slightly screwy order based on its keys (which are the users' usernames). I can forcibly send a array_values() to the JSON output, but it breaks Masquerade's functionality as it requires the username to be the key. I'd imagine this would be a problem with other autocompletion stuff in Drupal that uses JSON loads.

Can anyone suggest a way of doing this please? I'm not sure whether it's a matter of Masquerade or of D7's textfield autocompletion.


  • Were you ever able to figure this out? I'm having the same problem with drupal_json_output messing with the order of my data. Aug 8, 2015 at 8:18
  • @richard this was a while ago and I can't remember really, sorry! Aug 8, 2015 at 8:56
  • No problem. Thanks for responding. I'm posting my solution below. Aug 8, 2015 at 21:37

2 Answers 2


I would think its the query that handles the ordering. You can alter the query that fetches the results for the autocomplete field. Find the query in the module code and it should have a tag associated with it. You can then alter the query by implementing hook_query_TAG_alter


This will allow you to change the orderby for the query

EDIT: Looking at the masquerade module I don't think it adds tags to the queries which means you cant mess with the ordering.

  • Hi, thanks for answering. The contrib module itself only does two things in this sense: a) defines the #autocomplete_path on the textfield, and b) defines the menu paths/json output to be supplied to it. I believe the rest of it is handled by Drupal! Feb 12, 2014 at 11:17
  • Whats the path its using for the autocomplete field?
    – nickwshaw
    Feb 12, 2014 at 11:21
  • The default uses /masquerade/autocomplete, my custom ones are using a few different paths such as /xyz_masquerade/autocomplete/intelligent. They all point to functions that do a simple drupal_json_output() of arrays with the format ('username' => 'full name/html output/etc') Feb 12, 2014 at 11:24
  • Ok so to be clear the order is correct before passing through drupal_json_output()?
    – nickwshaw
    Feb 12, 2014 at 11:48
  • Yes, that is correct (I have to be verbose for SO's minimum comment length...) Feb 12, 2014 at 12:47

I figured out a workaround. I used a unique string for the ID column of my entity table.

I don't think there is a way to have a numerical value as the index that gets used by drupal_json_output(), even if the data type for the column in the database is varchar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.