Steps to reproduce:

- Enable views, taxonomy, content translation
- Enable the languages German and French
- Create a vocabulary with these terms and the corresponding translations:

- Albania / Albanien / Albanie
- Egypt / Ägypten / Égypt
- Finland / Finnland / Finlande
- Germany / Deutschland / Allemagne
- Saudi Arabia / Saudi-Arabien / Arabie saoude

- Create a view with an exposed taxonomy term filter.
- Open the view in English, German and French.

Expected ordering of the taxonomy terms:

English: Albania, Egypt, Finland, Germany, Saudi Arabia
German: Ägypten, Albanien, Deutschland, Finnland, Saudi-Arabien
French: Albanie, Allemagne, Arabie saoude, Égypt, Finlande

Actual ordering in all languages:

Egypt, Albania, Germany, Saudi Arabia, Finland

As can be seen, each term is in the first position allowed by any of its translations: Egypt by its German name, Germany and Saudi Arabia by their French names.

It seems that in the query, the terms are selected without language filtering, ordered alphabetically, and then the TIDs are de-duplicated before being passed to Term::loadMultiple.

The fix would be inserting a langcode filter into the entity query.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cburschka created an issue. See original summary.

cburschka’s picture

Issue summary: View changes
dagmar’s picture

There is a similar issue #1821274: Add back ability to sort on vocabulary weight and name that was closed without explanation. Actually there is some relevant code at TaxonomyIndexTid

      else {
        $options = [];
        $query = \Drupal::entityQuery('taxonomy_term')
          // @todo Sorting on vocabulary properties -
          //   https://www.drupal.org/node/1821274.
          ->sort('weight')
          ->sort('name')
          ->addTag('taxonomy_term_access');
        if ($this->options['limit']) {
          $query->condition('vid', $vocabulary->id());
        }
        $terms = Term::loadMultiple($query->execute());
        foreach ($terms as $term) {
          $options[$term->id()] = \Drupal::entityManager()->getTranslationFromContext($term)->label();
        }
      }
cburschka’s picture

Indeed, the current approach is broken because it doesn't even present the terms correctly in a single default language. It's not like "we're in language X, but the terms are sorted by their language Y translations". Rather "all translations from languages X,Y,Z are sorted in one list, then all but the first occurrence of each term are discarded", which yields gibberish.

As the translation of each term is not acquired until later, it is impossible to apply any ordering in the entity query; the sorting has to be done in PHP on the results.

        foreach ($terms as $term) {
          $options[$term->id()] = \Drupal::entityManager()->getTranslationFromContext($term)->label();
        }
        // only now do we actually know what the term names are, and how to sort them.
cburschka’s picture

I would suggest the following, but it can probably be improved on:

        $weighted = [];
        $query = \Drupal::entityQuery('taxonomy_term')
          ->addTag('taxonomy_term_access');
        if ($this->options['limit']) {
          $query->condition('vid', $vocabulary->id());
        }
        /** @var \Drupal\taxonomy\TermInterface[] $terms */
        $terms = Term::loadMultiple($query->execute());
        foreach ($terms as $term) {
          $weighted[$term->getWeight()][$term->id()] = \Drupal::entityManager()
                                                              ->getTranslationFromContext($term)
                                                              ->label();
        }
        ksort($weighted);
        $options = [];
        foreach ($weighted as $weight => $weight_options) {
          asort($weight_options);
          $options += $weight_options;
        }
cburschka’s picture

Status: Active » Needs review
FileSize
1.85 KB

Patch is for 8.2.7, but as far as I can tell this should still apply to 8.3.x.

Status: Needs review » Needs work

The last submitted patch, 6: drupal-taxonomy-filter-sort-2862533.patch, failed testing.

dagmar’s picture

@cburschka It will not apply to 8.3.x because array() syntax was replaced by [] syntax.

gaurav.kapoor’s picture

Assigned: Unassigned » gaurav.kapoor
gaurav.kapoor’s picture

Assigned: gaurav.kapoor » Unassigned
Status: Needs work » Needs review
FileSize
1.72 KB

Re-roll

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.

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.

florianmuellerCH’s picture

florianmuellerCH’s picture

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

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

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

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.

tomasbarej’s picture

Here is patch version for 8.9.x

Status: Needs review » Needs work

The last submitted patch, 18: 2862533-18-taxonomy-filter-sort.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
1.99 KB
792 bytes

Updated the deprecated function.

florianmuellerCH’s picture

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.

JeroenT’s picture

Status: Needs review » Closed (duplicate)
JeroenT’s picture

Issue tags: +Bug Smash Initiative