Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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.

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.

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

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');
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top