I'm using entity_translation to translate both nodes and taxonomy terms and, from what I've read, entity_translation will be packaged in D8 core for translations. My taxonomy terms are translated properly everywhere without needing to use i18n_taxonomy, but NOT in views exposed filters.

I will propose a patch which uses entity_label instead of term names to display term labels in exposed filters.


guillaumev’s picture

Status: Active » Needs review
1.51 KB
PASSED: [[SimpleTest]]: [MySQL] 1,458 pass(es). View

Here is the patch. There might be a few performance issues, but on my site, it allows me to have taxonomy terms translated in exposed filters without requiring i18n_taxonomy or i18nviews...

dawehner’s picture

1.93 KB
PASSED: [[SimpleTest]]: [MySQL] 1,458 pass(es). View

I guess it would be a bit easier to read if we name the variable $term, because we already know the type of entity here.

Changed admin_summary() to reflect this as well.

TuWebO’s picture

Patch #2 worked for me.

I've tested it in different languages and I didn't find any issues at all.
Looks pretty clean solution, isn't it?

TuWebO’s picture

Upps, I found some notice here, look at line 124 of views_handler_filter_term_node_tid.inc

        if ($tree) {
          foreach ($tree as $term) {
            $term = taxonomy_term_load($term->tid);
            $choice = new stdClass();
            $choice->option = array($term->tid => str_repeat('-', $term->depth) . entity_label('taxonomy_term', $term));
            $options[] = $choice;

Seems like $term variable in the foreach has some more values than $term variable that loads the taxonomy term. Since they have same names, $term->depth will throw a notice because it doesn't exist.

I will try patch in #1, seems like it is taken care of this, and report here later on.

TuWebO’s picture

1.95 KB
PASSED: [[SimpleTest]]: [MySQL] 1,472 pass(es). View

I have made a patch that combines both #1 and #2. I think we should keep $term as is, and add $entity as in #1. But #2 has one more change in line 352 that #1 hasn't, so I added it.

dimakopu’s picture

Status: Needs review » Active

Patch #5 worked for me! Thanks a lot!

TuWebO’s picture

Status: Active » Needs review

Changing status to needs review.

plach’s picture

Status: Needs review » Needs work

You should be using entity_load_multiple() instead of taxonomy_term_load(): the former should be quite more performant in this case.

TuWebO’s picture

Hi plach,
Thanks for the advice. Since entity_load_multiple seems to be available only for D8, I think this one could do the work:

As soon as I have something I will post it here.

plach’s picture

Yeah, sorry, that will work too. I meant entity_load() :)

TuWebO’s picture

2.89 KB
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es). View

Hi all,
I've just follow the @plach comment and have come with an awesome performance improvement.
I have tested it with a 4.000 taxonomy terms vocabulary (didn´t use two languages since I am only testing performance issues between the different code), with a simple view (with and without hierarchy dropdown exposed filters) showing 10 nodes out of 2000.

Using Views "as is"

This means that taxonomy terms won´t be translated. The fastest way.
Executed: 125 quieries in 62,6ms

Using Views with 1651726-entity_label-5.patch

This means that taxonomy terms will be translated, but huge performance issue could come. That's bad.
Executed: 7979 quieries in 1433,54ms

Using Views 1651726-entity_label-11.patch (the one attached here)

This means that taxonomy terms will be translated using taxonomy_term_load_multiple(), but you might expect increase the execution sql time by three. This is the best alternative I've tested by now if I want to have the terms translated.
Executed: 124 quieries in 190ms

I have also tried entity_load(), and it worked as fine as taxonomy_term_load_multiple(), I have used taxonomy function because I think it is more clear, but feel free to modify it too if you think it is better entity_load().

I also think that the code from the patch could be better, so feel free to modify it or comment around this issue.

I have some questions:

  1. In this piece of code
    -        $result = db_select('taxonomy_term_data', 'td')
    -          ->fields('td')
    -          ->condition('td.tid', $this->value)
    -          ->execute();
    -        foreach ($result as $term) {
    +        $result = taxonomy_term_load_multiple($this->value);
    +        foreach ($result as $entity_term) {

    I replaced the db_select for a taxonomy_load_multiple(), do you guys see any problem with this? (I don´t know, security reasons or whatever you can think of)

  2. Also, filling the $tids array for taxonomy_load_multiple() function, needs an extra foreach, maybe there is a better php function that could do the work, but I couldn't find it.
TuWebO’s picture

Status: Needs work » Needs review

Sorry, changing status

plach’s picture

Crell’s picture

Related and should be merged, but there's some differences in what they cover. This issue has more followers, so I'd suggest merging the other issue here.

TuWebO’s picture

3.88 KB
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es). View

I have red the #1857608: Translation doesn't affect exposed term filters due to bypassing entity system and I added some logic and functionality from there (@Crell patch):
1 - Logic for getting the taxonomy terms entities
2 - Changing db_select for EntityFieldQuery

I have one question in the number 2:
Using EntityFieldQuery we can add $query->addTag('term_access'), so I added it. @Crell or anybody see any problems with it? I know that there is some access problems with views, but I don´t know if adding this tag could cause any problems.

So I have made a new patch adding @crell's functionality and marked #1857608: Translation doesn't affect exposed term filters due to bypassing entity system as duplicated.

Please review, test it, change it or any other things will be very welcome.

d3claes’s picture

Hi, I have applied the patch and it works fine.
The var $tids at line 159 is undefined:

Notice: Undefined variable: tids in views_handler_filter_term_node_tid->value_form() (line 159 of ...\sites\all\modules\views\modules\taxonomy\views_handler_filter_term_node_tid.inc).

I added $tids[] = $term->tid; in the foreach loop just above in line 156;

Thanks for the patch; taxonomy terms are translated correct.

Volx’s picture

3.68 KB
PASSED: [[SimpleTest]]: [MySQL] 1,627 pass(es). View

Lines 159-163 are superfluous anyway, they do the same as the foreach loop above. I rerolled patch #15 using taxonomy_term_load_multiple.

dawehner’s picture

Just wondering: What is the reason why we now even use EFQ, thought we did not in #11
Even yeah it's in the same spot, it seems to be that we don't need this convertion here? Afaik there is another issue which does exactly that.

quasi’s picture

JonathanHindi’s picture

I tested Patch #17 Working fine for me, But I can't test on a large number of terms to check the performance issues.

dawehner’s picture

Status: Needs review » Needs work

No feedback on #18 yet :(

Berdir’s picture

See also #1864870: Use entity_label() to display taxonomy terms, which I think is more or less duplicates this, without doing query conversions.

drupalerocant’s picture

I tested Patch #17 on Views 7.x-3.7 on a web with 3 languages and localized terms.
Working fine at the moment on an exposed filter of 10 terms.
Before this approach i tried the suggested patch on https://drupal.org/node/1864870#comment-7775243 but didn't work.

Stevel’s picture

Status: Needs work » Needs review
2.96 KB
PASSED: [[SimpleTest]]: [MySQL] 1,652 pass(es). View

Re-rolled according to #18.

drupalina’s picture

Just like drupalerocant in#23, I first tried the patch in that link (which didn't work), and then tried the patch here in #24 on my stable version of Views-7x-3.7 and it works beautifully - the terms in my exposed filters are all translated now. I hope that this patch will be included in the next release of Views. Thanks, Stevel!

Stevel’s picture

2.99 KB
PASSED: [[SimpleTest]]: [MySQL] 1,652 pass(es). View
+++ b/modules/taxonomy/views_handler_filter_term_node_tid.inc
@@ -126,9 +123,15 @@ class views_handler_filter_term_node_tid extends views_handler_filter_many_to_on
+          $entities = taxonomy_term_load_multiple($tids);
+          foreach ($tree as $entity_term) {

Loading the terms but then not using the loaded terms, won't do any good... Updated patch.

Stevel’s picture

Issue tags: +Needs tests

This obviously needs some tests.

palamida’s picture

Version: 7.x-3.x-dev » 7.x-3.7

#26 so far working ok

Notice: Undefined property: stdClass::$depth in views_handler_filter_term_node_tid->value_form() (line 134 of /sites/all/modules/views/modules/taxonomy/views_handler_filter_term_node_tid.inc).

Using #17 -> Dosen't work on taxonomy term (translated) fields (generated from i18n views) but only on classic taxonomy term fields


Stevel’s picture

2.99 KB
PASSED: [[SimpleTest]]: [MySQL] 1,627 pass(es). View

An updated patch. The $depth property is set by taxonomy_get_tree, but not by taxonomy_term_load_multiple. Changed the patch to load entities with taxonomy_get_tree.

plach’s picture

Version: 7.x-3.7 » 7.x-3.x-dev
Status: Needs review » Reviewed & tested by the community

Looks good and works as advertised.

ptmkenny’s picture

Seconding the RTBC. This solves an issue with the title module as well, which I have marked as a duplicate: #1540790: Views exposed filters - is there a way to make translated taxonomy terms display based on page language?

MXT’s picture

Patch provided in #29 works very well for me.

Can this be committed please?

Thank you very much

gghh2’s picture

Patch provided in #29 it's ok for me too..
(Domain access, Entity translation (without taxonomy i18n).

Thanks soo much !

Stevel’s picture

Issue summary: View changes

Hide old versions of the patch.

Stevel’s picture

Hide some even older versions as well.

mr.york’s picture

#29 works very well for me.
Thank you.

dawehner’s picture

Project: Views » Drupal core
Version: 7.x-3.x-dev » 8.x-dev
Component: Translations » views.module
Category: Feature request » Bug report
Status: Reviewed & tested by the community » Patch (to be ported)

Thank you very much. Committed to 7.x-3.x

We might have to do something similar in drupal 8, on which this is clearly a bug.

vaccinemedia’s picture

Close, however, there are o fallbacks. So if there isn't a translated version of a term nothing appears. I've attached a screen grab of switching from English to French where only the first term has been translated.

Anonymous’s picture

I have found a big problem:

diff --git a/modules/taxonomy/views_handler_filter_term_node_tid.inc b/modules/taxonomy/views_handler_filter_term_node_tid.inc
index 7eb868f..62e6a2b 100644

--- a/modules/taxonomy/views_handler_filter_term_node_tid.inc
+++ b/modules/taxonomy/views_handler_filter_term_node_tid.inc

@@ -147,8 +145,14 @@ class views_handler_filter_term_node_tid extends views_handler_filter_many_to_on
         $result = $query->execute();
+        $tids = array();
         foreach ($result as $term) {
-          $options[$term->tid] = $term->name;
+          $tids[] = $term->tid;
+        }
+        $entities = taxonomy_term_load_multiple($tids);
+        foreach ($entities as $entity_term) {
+          $options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);

The second and new foreach-loop kills my performance and the view has very long to load.

See here for more informationen: https://drupal.org/node/2277811.

kopeboy’s picture

Guys, this still doesn't work in Drupal 7 if it's used on a Search Index view display.

Using Views 3, and i18nviews dev (but I don't think it's related).

abuelitovaldez’s picture

weird, in views preview (backend) extended filters are translated and on the website (frontend) extended filters all empty. aren't there the same functions being called???

jibran’s picture

Project: Drupal core » Views
Version: 8.0.x-dev » 7.x-3.x-dev
Component: views.module » Translations
Status: Patch (to be ported) » Fixed

I think this is already fixed in D8.
$default .= entity_label('taxonomy_term', $entity_term);
'#default_value' => EntityAutocomplete::getEntityLabels($terms)
$choice->option = array($term->tid => str_repeat('-', $term->depth) . entity_label('taxonomy_term', $term));
$choice->option = array($term->id() => str_repeat('-', $term->depth) . SafeMarkup::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label()));
$options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
$options[$term->id()] = SafeMarkup::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label());
$this->value_options[$entity_term->tid] = entity_label('taxonomy_term', $entity_term);
$this->valueOptions[$term->id()] = SafeMarkup::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label());
So moving back to views queue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.