vdchristelle
9/9/2013 - 7:58 AM

A fix for Taxonomy translation bug in views.

A fix for Taxonomy translation bug in views.


A fix for Taxonomy translation bug in views.

Views doesn't filter on taxonomy language. This custom module adds the taxonomy language field as an extra condition to your views query.
This is what you should do:

1. Open taxlangfix.module
2. Find "PUT_YOUR_VIEW_NAME_IN_HERE"
3. Replace it by your view name (not the block or page name)
4. Save the .module file, clear your cache and you're done!

Do you have several views that need the language filter?
Just copy/paste the if statement in the query alter function.

Good luck!
Curious about the code? These links might be usefull ;)

- In general: function hook_query_alter:      http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_query_alter
- For views: function hook_views_query_alter: http://api.drupal.org/api/views/views.api.php/function/hook_views_query_alter/7

name = "Taxonomy language fix"
description = "Module adds taxonomy language filter to views query."
core = "7.x"
package = Custom
php = "5.2.4"
version = "7.x-1.0"
project = "Taxonomy fix"

dependencies[] = "views"
dependencies[] = "taxonomy"
;dependencies[] = "views_ui"
;features[ctools][] = "ds:ds:1"
;features[ctools][] = "linkit:linkit_profiles:1"
;features[ctools][] = "strongarm:strongarm:1"
;features[ctools][] = "views:views_default:3.0"
;features[ds_field_settings][] = "field_collection_item|field_slide|banner_large"
;features[ds_field_settings][] = "field_collection_item|field_slide|banner_small"
;features[ds_layout_settings][] = "node|slider|default"
;features[ds_view_modes][] = "banner_large"
;features[ds_view_modes][] = "banner_small"
;features[field][] = "field_collection_item-field_slide-field_banner_image"
;features[field][] = "field_collection_item-field_slide-field_banner_title"
;features[field][] = "node-slider-field_slide"
;features[image][] = "960_large"
;features[image][] = "960_small"
;features[linkit_profiles][] = "default"
;features[node][] = "slider"
;features[variable][] = "ds_extras_field_template"
;features[variable][] = "ds_extras_fields_extra"
;features[variable][] = "ds_extras_fields_extra_list"
;features[variable][] = "ds_extras_switch_view_mode"
;features[variable][] = "field_bundle_settings_field_collection_item__field_slide"
;features[variable][] = "menu_options_slider"
;features[variable][] = "menu_parent_slider"
;features[variable][] = "node_options_slider"
;features[variable][] = "node_preview_slider"
;features[variable][] = "node_submitted_slider"
;features[views_view][] = "banner"
<?php
/**
 * @file
 * Custom code for your dupal 7 site
 */

/**
 * Implements hook_help().
 */
function taxlangfix_help($path, $arg) {
  switch ($path) {
    case 'admin/help#custom':
      //$output = file_get_contents(drupal_get_path('module', 'the_aim_custom') . '/README.txt');
      //return nl2br($output);
      return t('<h1>A fix for Taxonomy translation bug in views.</h1>
      <p>Views doesn\'t filter on taxonomy language. This custom module adds the taxonomy language field as an extra condition to your views query.</p>
      <h2>This is what you should do:</h2>
      <ol>
      <li>Open taxlangfix.module</li>
      <li>Find "PUT_YOUR_VIEW_NAME_IN_HERE"</li>
      <li>Replace it by your view name (not the block or page name)</li>
      <li>Save the .module file, clear your cache and you\'re done!</li>
      </ol>
      <p>Do you have several views that need the language filter?<br />Just copy/paste the if statement in the query alter function.</p>
      <p>Good luck!</p>
      <h2>Curious about the code? These links might be usefull ;)</h2>
      <ul>
      <li>In general: <a href="http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_query_alter" target="_blank">function hook_query_alter($query)</a></li>
      <li>For views: <a href="http://api.drupal.org/api/views/views.api.php/function/hook_views_query_alter/7" target="_blank">function hook_views_query_alter($view, $query)</a></li>
      </ul>
      ');
  }
}

/**
 * Implements hook_views_query_alter
 * FIX TAXONOMY
 * Add language filter to views
 */
function taxlangfix_views_query_alter(&$view, &$query) {
  if ($view->name == 'PUT_YOUR_VIEW_NAME_IN_HERE') {
    $query->where[] = array(
      'conditions' => array(array(
        'field' => 'taxonomy_term_data.language',
        'value' => array('***CURRENT_LANGUAGE***'),
        'operator' => 'in',
      )),
      'args' => array(),
      'type' => 'AND',
    );
  }
}