9

I have searched a way to filter the taxonomy terms by language (as done for nodes), but I didn't find any way to do it.

Does the Views module support that filter in taxonomies too, or just in nodes?
How can I filter the taxonomy terms by language?

  • 1. use 2 groups in vocabulary 2. use hook_form_alter to declare group to users use – dobeerman Apr 7 '11 at 9:15
  • i don't understand what is the user has to do with pickup group of taxonomy. the site is in multi languages fro example A lang , B lang , C lang and when the user go to B lang i just want the taxonomies displayed based on this language and so on in other language. not that i translate the taxonomies terms for each enabled language. – mohamad salama Apr 7 '11 at 11:15
  • # can i edit the query string in the view !? or its just for notifying me . if its where is the place for editing it ? – mohamad salama Apr 7 '11 at 11:19
11

By enabling the module Internationalization Views, the option to filter the taxonomy term by the language is available. Note that the version is currently in dev but it works like a charm.

screenshot showing effect of installing i18nviews

  • 2
    Note that this filter will only become available if you have also enabled module i18n_taxonomy. That is, if you use node translation for taxonomy terms. – tanius Sep 18 '14 at 21:33
3

You can use the Internationalization Views module in combination with the Taxonomy Translation module (which is part of Internationalization) to get a language filter for taxonomy terms.

Translate views using Internationalization. This is a work in progress and not all Views properties can be translated yet. Requires Internationalization 6.x-1.5 or newer.

2

Here is another solution:

function mymodule_views_query_alter(&$view, &$query) {
  if ($view->name == 'yourviewmachinename') {
    $query->where[] = array(
      'conditions' => array(array(
        'field' => 'taxonomy_term_data.language',
        'value' => array('***CURRENT_LANGUAGE***'),
        'operator' => 'in',
      )),
      'args' => array(),
      'type' => 'AND',
    );
  }
}

Source here.

1

The Views module doesn't allow to filter the taxonomy terms by the language associated with it, as Drupal only associate a language with nodes.

You can set the view to show only nodes with a predefined language, though.

  • Under "Filter criteria," select "Add"
  • in the next form, select "Content translation: Language"
  • the next form will allow you to select which language to use to filter the view content between "Select all," "Current user's language," "Default site language," "No language," and one of the languages enabled in the site.

I added this filter on the taxonomy term view that comes with the Views module, selecting English as language to filter the content, and I created two nodes: one in English, and one in Latin. I assigned to both the nodes the same taxonomy term, the one with ID equal to 22.
When I visited http://example.com/taxonomy/term/22, the view effectively shown just the content in English.

  • its really weird in drupal 5 i can filter taxonomies by language easily. but any way thanks for sharing answers. – mohamad salama Apr 11 '11 at 8:02
0

Language filtering for entity translation. A language filter in the view's "Filter Criteria" section is only needed if you use the node translation system. If instead you use the more modern Entity Translation system available for Drupal 7, just adapt the "Field Language" setting in the view's "Other" section.

How to do it for taxonomy terms. I tried this for taxonomy terms and it works. Just make sure you add the correct one of the two field instances for every translatable field, namely the one with a description of "Appears in: taxonomy-term:your-vocab-name". For details, see on issue #1841434.

0

Add to your YOUR_MODULE.module

function YOUR_MODULE_views_data_alter(&$data) {
  $opts['title'] = 'Language';
  $opts['table'] = 'taxonomy_term_data';
  $opts['help'] = 'Taxonomy term language.';
  $opts['filter']['handler'] = 'YOUR_MODULE_handler_filter_language';
  $data['taxonomy_term_data']['language'] = $opts;
}

Add to your YOUR_MODULE.info

files[] = views/handlers/filter/YOUR_MODULE_handler_filter_language.inc

Create File "views/handlers/filter/YOUR_MODULE_handler_filter_language.inc" within your module directory and place next content:

/**
 * @file
 * Views handler to filter language by term.
 */

/**
 * Filter by submission status
 */
class YOUR_MODULE_handler_filter_language extends views_handler_filter_in_operator {
  function get_value_options() {
    $languages = array(
      '***CURRENT_LANGUAGE***' => t("Current user's language"),
      '***DEFAULT_LANGUAGE***' => t("Default site language"),
      LANGUAGE_NONE            => t('Language neutral'),
    );
    $this->value_title = t('Language');
    $options = array_merge($languages, locale_language_list());
    $this->value_options = $options;
  }

  // '0' won't work as a key for checkboxes.
  function value_form(&$form, &$form_state) {
    parent::value_form($form, $form_state);
    $form['value']['#type'] = 'select';
  }
}

Clear All caches and New criterion will appear.

Or use my module Akuma Taxonomy

0

As I'm having some other issues with Internationalization Views module, namely that it seems to alter allready translated UI from views (pager directions, header / footer text etc.), I liftet the relevant code for creating the language filters out in a separate module. Replace MYMODULE below with the name of your module. Works as a charm for me!

/**
 * Code below is lifted from the i18nviews module. Gives the possibility to filter for
 * language on term views
 *
 * Implementation of hook_views_data_alter().
 *
 * Registers views handlers with dependency to i18n_taxonomy.
 */
function MYMODULE_views_data_alter(&$data) {

  // Add i18n language field to taxonomy_term_data. No clash.
  $data['taxonomy_term_data']['language'] = array(
    'group' => t('Taxonomy term'),
    'title' => t('Language'),
    'help' => t('The language the term is in.'),
    'field' => array(
      'handler' => 'MYMODULE_handler_field_taxonomy_term_language',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'MYMODULE_handler_filter_taxonomy_term_language',
    ),
    'argument' => array(
      'handler' => 'MYMODULE_handler_argument_taxonomy_term_language',
    ),
  );
}

/**
 * Field handler to provide simple renderer that shows term language.
 */
class MYMODULE_handler_field_taxonomy_term_language extends views_handler_field_taxonomy {
  function render($values) {
    $languages = locale_language_list();
    $value = isset($languages[$values->{$this->field_alias}]) ? $languages[$values->{$this->field_alias}] : '';
    $value = $this->get_value($values);
    return $this->render_link($this->sanitize_value($value), $values);
  }
}

/**
 * Filter by language
 */
class MYMODULE_handler_filter_taxonomy_term_language extends views_handler_filter_in_operator {
  function get_value_options() {
    if (!isset($this->value_options)) {
      $this->value_title = t('Language');
      $languages = array(
        '***CURRENT_LANGUAGE***' => t("Current user's language"),
        '***DEFAULT_LANGUAGE***' => t("Default site language"),
        LANGUAGE_NONE => t('No language')
      );
      $languages = array_merge($languages, locale_language_list());
      $this->value_options = $languages;
    }
  }
}

/**
 * Argument handler to accept a language.
 */
class MYMODULE_handler_argument_taxonomy_term_language extends views_handler_argument {
  function construct() {
    parent::construct('language');
  }

  /**
   * Override the behavior of summary_name(). Get the user friendly version
   * of the language.
   */
  function summary_name($data) {
    return $this->term_language($data->{$this->name_alias});
  }

  /**
   * Override the behavior of title(). Get the user friendly version of the
   * node type.
   */
  function title() {
    return $this->term_language($this->argument);
  }

  function term_language($langcode) {
    $languages = locale_language_list();
    return isset($languages[$langcode]) ? $languages[$langcode] : t('Unknown language');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.