Getting Selectize to honor a weight field in sort with Drupal or maybe at all

Here’s the “beautified” version :-/

{ “path”: { “baseUrl”: “/”, “scriptPath”: null, “pathPrefix”: “”, “currentPath”: “search”, “currentPathIsAdmin”: false, “isFront”: false, “currentLanguage”: “en”, “currentQuery”: { “types”: [ “findit_program”, “findit_organization” ] } }, “pluralDelimiter”: “\u0003”, “ajaxPageState”: { “libraries”: “admin_toolbar/toolbar.tree,admin_toolbar_tools/toolbar.icon,coffee/drupal.coffee,contacts/contacts.toolbar,contextual/drupal.contextual-links,contextual/drupal.contextual-toolbar,core/drupal.active-link,core/html5shiv,encontrarlo_toolbar/admin,microformats/microformats,newtowne/global,quickedit/quickedit,selectize/core,selectize/drupal,shortcut/drupal.shortcut,svg_select/base,system/base,toolbar/toolbar,toolbar/toolbar.escapeAdmin,tour/tour,user/drupal.user.icons”, “theme”: “newtowne”, “theme_token”: null }, “ajaxTrustedUrl”: { “form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwcfZl2uzM”: true, “/search”: true }, “ckeditorheight”: { “offset”: 1, “line_height”: 1.5, “unit”: “em”, “disable_autogrow”: false }, “coffee”: { “maxResults”: 7 }, “selectize”: { “edit-services”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":false,"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":false,"placeholder":"Select one or more services"}”, “edit-activities”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":false,"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":false,"placeholder":"Select one or more activities"}”, “edit-cost-subsidies”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":false,"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":false,"placeholder":"Choose a cost subsidy option"}”, “edit-ages”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":[{"field":"weight","direction":"asc"}],"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":true,"placeholder":"Select one or more ages","options":[{"text":"Pre-natal","value":"Pre-natal","weight":"-1","synonyms":null},{"text":"Infant","value":"Infant","weight":"0","synonyms":null},{"text":"1","value":"1","weight":"1","synonyms":null},{"text":"2","value":"2","weight":"2","synonyms":null},{"text":"3","value":"3","weight":"3","synonyms":null},{"text":"4","value":"4","weight":"4","synonyms":null},{"text":"5","value":"5","weight":"5","synonyms":null},{"text":"6","value":"6","weight":"6","synonyms":null},{"text":"7","value":"7","weight":"7","synonyms":null},{"text":"8","value":"8","weight":"8","synonyms":null},{"text":"9","value":"9","weight":"9","synonyms":null},{"text":"10","value":"10","weight":"10","synonyms":null},{"text":"11","value":"11","weight":"11","synonyms":null},{"text":"12","value":"12","weight":"12","synonyms":null},{"text":"13","value":"13","weight":"13","synonyms":null},{"text":"14","value":"14","weight":"14","synonyms":null},{"text":"15","value":"15","weight":"15","synonyms":null},{"text":"16","value":"16","weight":"16","synonyms":null},{"text":"17","value":"17","weight":"17","synonyms":null},{"text":"18","value":"18","weight":"18","synonyms":null},{"text":"19","value":"19","weight":"19","synonyms":null},{"text":"20s","value":"20s","weight":"20","synonyms":null}],"searchField":["text","synonym"]}” }, “toolbar”: { “breakpoints”: { “toolbar.narrow”: “only screen and (min-width: 16.5em)”, “toolbar.standard”: “only screen and (min-width: 38.125em)”, “toolbar.wide”: “only screen and (min-width: 61em)” }, “subtreesHash”: “HGWoQAJBT-_9Gk4GrR67QYxtCSYVFSRE7oWLT0lhEUY” }, “user”: { “uid”: “1”, “permissionsHash”: “c880a39485be428adae58337b422fe2693ce5529fd5e425d5df2a6c04643ed1f” } }

Now passing in just ‘weight’, not even in an array.

{ “path”: { “baseUrl”: “/”, “scriptPath”: null, “pathPrefix”: “”, “currentPath”: “search”, “currentPathIsAdmin”: false, “isFront”: false, “currentLanguage”: “en”, “currentQuery”: { “types”: [ “findit_program”, “findit_organization” ] } }, “pluralDelimiter”: “\u0003”, “ajaxPageState”: { “libraries”: “admin_toolbar/toolbar.tree,admin_toolbar_tools/toolbar.icon,coffee/drupal.coffee,contacts/contacts.toolbar,contextual/drupal.contextual-links,contextual/drupal.contextual-toolbar,core/drupal.active-link,core/html5shiv,encontrarlo_toolbar/admin,microformats/microformats,newtowne/global,quickedit/quickedit,selectize/core,selectize/drupal,shortcut/drupal.shortcut,svg_select/base,system/base,toolbar/toolbar,toolbar/toolbar.escapeAdmin,tour/tour,user/drupal.user.icons”, “theme”: “newtowne”, “theme_token”: null }, “ajaxTrustedUrl”: { “form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwcfZl2uzM”: true, “/search”: true }, “ckeditorheight”: { “offset”: 1, “line_height”: 1.5, “unit”: “em”, “disable_autogrow”: false }, “coffee”: { “maxResults”: 7 }, “selectize”: { “edit-services”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":false,"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":false,"placeholder":"Select one or more services"}”, “edit-activities”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":false,"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":false,"placeholder":"Select one or more activities"}”, “edit-cost-subsidies”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":false,"direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":false,"placeholder":"Choose a cost subsidy option"}”, “edit-ages”: “{"create":false,"createOnBlur":false,"hideTermId":false,"createFilter":null,"delimiter":",","delimiter_warning":false,"sortField":[{"field":"weight","direction":"asc"}],"sortDirection":"asc","allowEmptyOption":false,"plugins":["remove_button"],"highlight":true,"maxItems":10,"persist":false,"diacritics":false,"closeAfterSelect":true,"placeholder":"Select one or more ages","options":[{"text":"Pre-natal","value":"Pre-natal","weight":"-1","synonyms":null},{"text":"Infant","value":"Infant","weight":"0","synonyms":null},{"text":"1","value":"1","weight":"1","synonyms":null},{"text":"2","value":"2","weight":"2","synonyms":null},{"text":"3","value":"3","weight":"3","synonyms":null},{"text":"4","value":"4","weight":"4","synonyms":null},{"text":"5","value":"5","weight":"5","synonyms":null},{"text":"6","value":"6","weight":"6","synonyms":null},{"text":"7","value":"7","weight":"7","synonyms":null},{"text":"8","value":"8","weight":"8","synonyms":null},{"text":"9","value":"9","weight":"9","synonyms":null},{"text":"10","value":"10","weight":"10","synonyms":null},{"text":"11","value":"11","weight":"11","synonyms":null},{"text":"12","value":"12","weight":"12","synonyms":null},{"text":"13","value":"13","weight":"13","synonyms":null},{"text":"14","value":"14","weight":"14","synonyms":null},{"text":"15","value":"15","weight":"15","synonyms":null},{"text":"16","value":"16","weight":"16","synonyms":null},{"text":"17","value":"17","weight":"17","synonyms":null},{"text":"18","value":"18","weight":"18","synonyms":null},{"text":"19","value":"19","weight":"19","synonyms":null},{"text":"20s","value":"20s","weight":"20","synonyms":null}],"searchField":["text","synonym"]}” }, “toolbar”: { “breakpoints”: { “toolbar.narrow”: “only screen and (min-width: 16.5em)”, “toolbar.standard”: “only screen and (min-width: 38.125em)”, “toolbar.wide”: “only screen and (min-width: 61em)” }, “subtreesHash”: “HGWoQAJBT-_9Gk4GrR67QYxtCSYVFSRE7oWLT0lhEUY” }, “user”: { “uid”: “1”, “permissionsHash”: “c880a39485be428adae58337b422fe2693ce5529fd5e425d5df2a6c04643ed1f” } }

That makes the apparently calculated-behind-myback ‘sortField’ look about right: {“field”: “weight, “direction”: “asc”}

OK (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.

https://www.drupal.org/project/selectize/issues/3102771#comment-13402729

Goddamn Drupal Selectize module is doing magic with the sortfield rather than taking the same arguments as documented for Selectize itself. It’s just confusing and annoying and totally undocumented.

Add 1,000 to the term weight so that it is never negative nor zero

Which is apparently needed for sort in Selectize.js to not totally screw up. Or to make it numeric instead of alphabetical? Whatever, gah.