Problem/Motivation

When using the preloaded entries options for the typeahead search on the select2 widget, the results are being displayed sorted by their entity id vs whatever sort is being returned by the handler. These seems due to how javascript is interpreting the preloaded entries array we're returning. It's treating it as an 'object' vs an 'array', which is causing it to sort the ids in ascending order, since it thinks those are just object properties now, whose order is unimportant.

Proposed resolution

Tweak the logic to return a sorted array made up of id/text objects instead. This why javascript interprets the overall collection as an array and won't alter the order they're being returned in.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jacobbell84 created an issue. See original summary.

matsbla’s picture

Status: Needs review » Needs work

I can confirm the problem, thanks for the patch!

However, if I have a reference field where several bundles are available, they will now only be sorted in each list separately; first one bundle alphabetically sorted, then the next. I guess all the items on both bundles should be sorted alphabetically together in one list?

jacobbell84’s picture

Status: Needs work » Needs review
FileSize
1.56 KB

Circling back to this. I added a sort on the back-end, can you see if it solves your issue with multiple bundles?

matsbla’s picture

Status: Needs review » Reviewed & tested by the community

I've tested again now, and looks great!

matsbla’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.