Problem/Motivation
one solution implementation suggestion in the Drupal.org Project issue tracking Issue queue in issue #2362065: Sort autocomplete of issue tags descending by usage count *in the issue metadata form* was to implement it in views, then we realized this was coming from core, not views. Which moved this to 8.0.x.
But, similar to #1120144: Term Reference Autocomplete widget - add option to match only start of term and expose settings (size, match) since #1847596: Remove Taxonomy term reference field in favor of Entity reference might happen, we might need to move this to a 7.x. And, 8.0.x would need a different entity reference solution.
In drupal 7, the sort for autocomplete results is alphabetical by the name/label of the term. This is an accident, that happens only because there is an index on the name. The sort should be more explicit. Split that off into a separate issue #2374301: Cannot predict the order of results in taxonomy autocompletion
Allowed in 8.x beta?
According to https://www.drupal.org/core/beta-changes
Maybe not. Because if this is a feature, well, it is just postponed to 8.1.x
It might allowed as a usability improvement, but only if the impact is greater than the disruption.
Proposed resolution
add a configuration option in the views UI settings page admin/structure/views/settings/advanced (defaulting to the current non-sorted by usage order).
and then use that config views_ajax_autocomplete_taxonomy() https://api.drupal.org/api/views/includes%21ajax.inc/function/views_ajax...
add a configuration option that would be per field.
sometimes, we do not know which entity type, bundle, field we are autocompleting on, and we only know the vocabulary id. This happens for example in the Views UI, where [fill this in to be accurate:] we add a filter by term on all content. In that cause, use a setting that would be per vocabulary.
Having two places to set sort behavior, might be overly confusing in the UI. Since a site building might set it in one place and not realize it could be overridden in another place. We might be able to solve that.
Remaining tasks
- update the issue summary explaining in more detail why the sort is currently by name.
User interface changes
Yes, a need configuration setting in the admin UI. On each field, on each vocabulary.
Behavior of autocomplete results could be sorted.
API changes
?
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | widget-sort.JPG | 23.14 KB | anybody |
| #21 | drupal-add_sort_for_taxonomy_autocomplete_widgets-2372225-21.patch | 6.92 KB | stborchert |
| #3 | 2372225.3.patch | 7.95 KB | yesct |
| #2 | 2372225.1.patch | 1.67 KB | yesct |
Comments
Comment #1
yesct commentedthis has the settings form.
and a start at putting the sort into the query.
no tests yes.
Comment #2
yesct commentedoops the file.
Comment #3
yesct commentednope, it's not views that provides the autocomplete results...
moving to core.
it's similar to #1120144: Term Reference Autocomplete widget - add option to match only start of term and expose settings (size, match)
no tests yet.
and a bunch of guessing, so probably very broken.
Comment #4
yesct commentedthis is particularly troubling. since the point seems to be to only need the vid, and I'm adding the entity, bundle and field to get the setting (since the setting is per field, per bundle)
Comment #5
yesct commentedMaybe a setting could be also on the vocabulary, which would be overridden at the field level. That might be tricky to communicate via the UI.
Comment #8
yesct commentedComment #9
yesct commentedMight not be allowed in 8.0.x according to https://www.drupal.org/core/beta-changes Added a section to the issue summary motivation to document that.
Comment #10
duaelfrI made some research on default MySQL ordering but it seems that there is no reliable rule. Everything seems to depend on the version and the configuration of the storage engine. It is possible that the vid_name index on the
taxonomy_term_datatable affects the default order of the results but this index has been moved to thetaxonomy_term_field_datatable in D8 so the queries are not the same so the results can differ (and they do).Refs :
In a way, given that the table structure changed between D7 and D8, that the database server can be different or have different versions, not explicitely adding an orderBy clause could be considered as a bug as there is no way to predict the result of the call. At least, we should add an orderBy('name') to ensure that the results are going to always be sorted by name.
Comment #11
duaelfrRelated issue opened to address the problem of the missing explicit orderBy clause.
#2374301: Cannot predict the order of results in taxonomy autocompletion
Comment #12
yesct commentedComment #13
jibranAs #1847596: Remove Taxonomy term reference field in favor of Entity reference landed so taxonomy auto complete is now a ER field. Few days back ER fixed #2412553: Taxonomy terms in an Entity Reference field are not sorted and this is almost opposite of that so I don't know how to respond here. Moving to ER queue.
Comment #14
duaelfr#2412553: Taxonomy terms in an Entity Reference field are not sorted is half true.
Currently, the specific ER implementation for taxonomy terms is bypassed for autocomplete (and autocomplete tags) widgets so the behavior is the same that other ER implementations. In these cases - that are part of the standard profile (tags field on article node type) - results are not sorted at all.
In #2374301: Cannot predict the order of results in taxonomy autocompletion I tried to force a default sort on names but I think it would be better to allow the admin to select the way he want the results sorted. I think that sort settings should be part of the EntityFormDisplay, not the FieldStorageConfig nor the FieldConfig.
Comment #17
jibranMoving to right component
Comment #21
stborchertHere is a rough proof-of-concept for this functionality to see if it is working and to get some ideas :)
There are some parts missing:
* key-value storage has to be cleared for the autocomplete widget after changing the settings
* settings-form lacks ajax
* tests
Comment #23
themic8 commentedstBorchert, Thanks, this proof of concept help me with a project.
Comment #28
anybodyStill a cool idea! Another option - giving the user the choice, would be to implement it as JavaScript functionality, which can be activated in certain field widgets.
That would be very flexible and allows the user to decide if and when and how to sort values.
This can be helpful in many cases, especially for unlimited references, and of course there are cases where you wouldn't want that.