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

?

Comments

yesct’s picture

Status: Active » Needs review

this has the settings form.

and a start at putting the sort into the query.

no tests yes.

yesct’s picture

StatusFileSize
new1.67 KB

oops the file.

yesct’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 8.0.x-dev
Component: taxonomy data » ajax system
StatusFileSize
new7.95 KB

nope, 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.

yesct’s picture

Component: ajax system » taxonomy.module
+++ b/core/modules/taxonomy/src/Controller/TermAutocompleteController.php
@@ -134,12 +137,18 @@ public function autocomplete(Request $request, $entity_type, $field_name) {
-  public function autocompletePerVid(Request $request, VocabularyInterface $taxonomy_vocabulary) {
+  public function autocompletePerVid(Request $request, VocabularyInterface $taxonomy_vocabulary, $entity_type, $bundle_name, $field_name) {

this 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)

yesct’s picture

Maybe 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.

The last submitted patch, 2: 2372225.1.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 3: 2372225.3.patch, failed testing.

yesct’s picture

Title: add sort setting for autocomplete results in views_ajax_autocomplete_taxonomy() » add sort setting for taxonomy term autocomplete results
Issue summary: View changes
yesct’s picture

Issue summary: View changes

Might 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.

duaelfr’s picture

I 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_data table affects the default order of the results but this index has been moved to the taxonomy_term_field_data table 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.

duaelfr’s picture

Related issue opened to address the problem of the missing explicit orderBy clause.
#2374301: Cannot predict the order of results in taxonomy autocompletion

yesct’s picture

Issue summary: View changes
jibran’s picture

Component: taxonomy.module » entity_reference.module

As #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.

duaelfr’s picture

#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.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jibran’s picture

Title: add sort setting for taxonomy term autocomplete results » Add sort setting for taxonomy term autocomplete results
Component: entity_reference.module » entity system

Moving to right component

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

stborchert’s picture

Here 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

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

themic8’s picture

stBorchert, Thanks, this proof of concept help me with a project.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
anybody’s picture

StatusFileSize
new23.14 KB

Still 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.

Sort widget JS

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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.