The "Term Name" argument validator which converts a term name into its corresponding term ID does not filter for terms by the current language. If two or more languages have the same term name, then results are returned for the wrong language when using term name is used as an argument (and if i18n content filtering is added to the view, then no results return at all).

I'll explain how I ran into the problem: I am creating an Ubercart store where certain items can be purchased from an artist's special collection. I've created a custom View that displays a grid of the products available based on the taxonomy term supplied as an argument to the View. The View has one argument (Taxonomy: Term ID) and the term name is converted to its corresponding term ID using the "Term Name/Synonym converted to Term ID" validator.

The path /en/products/category_name works fine because the term category_name is different in each language.
The path /en/products/artists_name actually returns the same thing as /fr/products/artists_name. If i18n content filtering is on, then /fr/products/artists_name returns English nodes, so no products appears in the View.

I have found a fix for this by overriding the stock validator plugin, but I don't know if there's a better way to approach this problem.

/**
 * Implementation of hook_views_plugins().
 */
function termvalidator_views_plugins() {
  return array(
    'module' => 'termvalidator',
    'argument validator' => array(
      'termvalidator_taxonomy_term' => array(
        'title' => t('Taxonomy term (i18n)'),
        'handler' => 'termvalidator_plugin_argument_validate_taxonomy_term',
        'path' => drupal_get_path('module', 'termvalidator') . '/includes',
        'parent' => 'taxonomy_term',
      ),
    ),
  );
}

and in termvalidator_plugin_argument_validate_taxonomy_term.inc:

class termvalidator_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate_taxonomy_term {
  function validate_argument($argument) {
    // Replace me with content from:
    // sites/all/modules/views/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc
    // copy from the beginning of the function until...
    case 'convert':
        global $language;
        $langcode = $language->language;
        $and = '';
        if (!empty($vids)) {
          $and = " AND td.vid IN(" . implode(', ', $vids) . ')';
        }
        if ($transform) {
          $result = db_fetch_object(db_query("SELECT td.* FROM {term_data} td LEFT JOIN {term_synonym} ts ON ts.tid = td.tid WHERE ((replace(td.name, ' ', '-') = '%s' OR replace(ts.name, ' ', '-') = '%s')$and) AND language='%s'", $argument, $argument, $langcode));
        }
        else {
          $result = db_fetch_object(db_query("SELECT td.* FROM {term_data} td LEFT JOIN {term_synonym} ts ON ts.tid = td.tid WHERE ((td.name = '%s' OR ts.name = '%s')$and) AND language='%s'", $argument, $argument, $langcode));
        }
        if (!$result) {
          return FALSE;
        }

        if ($type == 'convert') {
          $this->argument->argument = $result->tid;
          $this->argument->validated_title = check_plain($result->name);
        }

        return TRUE;
    }
  }
}

Comments

Anonymous’s picture

subscribing

intyms’s picture

subscribing

Bartezz’s picture

subscribing

miro_dietiker’s picture

Referencing to somehow similar-solution issue:
#947468: Translate Taxonomy Term Argument

benklocek’s picture

Subscribe