Big picture: I'm trying to get Selectize.js to use the default, source code source order for its initial listing (i don't really care if the order changes once filtering begins, but when the dropdown is first shown it should be the taxonomy terms in the order they have been sorted by weight).
Selectize doesn't have an option to do that, but a number of workarounds are suggested, all of which involve overriding the default 'score' function.
However, no matter what i pass in as the value of 'score' when customizing Selectize's settings (per below) i always get a JavaScript error "TypeError: f.settings.score.apply is not a function".
$form['ages'] = [
'#type' => 'selectize',
'#settings' => Selectize::settings([
'plugins' => ['remove_button'],
'placeholder' => t('Select one or more ages'),
'score' => 'function() { return function() { return 1 }; }',
]),
'#title' => $this->t('Ages'),
'#options' => $ages_options,
'#multiple' => TRUE,
];
The project page says "You can even override the scoring function used for sorting if you want to get crazy." So my question is... how do i do that?
And given that most Drupal-provided data could have a weight, we might want to honor Drupal taxonomy term weights, the order in string list fields, and such by default. But that's a different issue; i'm making this form myself here quite independent from Drupal fields and just need the technical capability to change the sort/score.
Thanks for any help!
Comments
Comment #2
mlncn commentedComment #3
mlncn commentedOK now i'm creating items that include weight and just trying to use the sortField option, and it's still not working, but no errors:
Comment #4
mlncn commentedOK (and i'm sure i went through this before), despite using the exact same key name ('sortField'), the Selectize Drupal module doesn't want actual Selectize setting (passed in as above) but just a simple string, which it turns into the setting structure above when it's passed in.
So now the values going into
$el.selectize(config);in selectize.drupal.js look correct, but apparently Selectize is still screwing up because it sure as heck isn't sorting by weight.Comment #5
mlncn commentedIt was either interpreting all the weights as alpha because of the presence of -1 and/or 0, or it just can't handle the concept of negative and/or zero numbers.
Adding 1,000 to the weight fixed it.
Again, i'm passing a value in to
sortField, which does not take the same values as documented in Selectize.js. This should either change or be documented by the Drupal module.Fortunately, i didn't need to override the score function. As far as i can tell the ability to pass in a function for score is still entirely broken.
In short, i'm setting
'sortField' => ['weight'],in the selectize settings and also passing into the selectize settings anoptionsarray to built from terms so that it looks like this:$options[] = ['text' => $term->name, 'value' => $term->name, 'weight' => $term->weight + 1000];(Note that we're building our own form element to use as a facet; i can see wanting a regular dropdown to respect term order but this wouldn't be quite how to do it. Here's the full context of what i'm doing.)