In a multi language site I use translated taxonomy terms (i18n) selected in a node field 'reference to term' and shown in a view with a term filter.

This works in a view with a normal exposed filter and with better exposed filter, the translated terms are shown as filter in the corresponding language.

---
Then I installed Views Filters Selective.
With the exposed filter 'term(select)' the filtering works fine (without translation of the term) in the default language.

If 'term(translated)(select)' is used as an exposed filter there is an error: 'No valid values found in filter', and the ajax error:

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /en/admin/structure/views/view/arrangementen/preview/page/ajax
StatusText: Service unavailable (with message)
ResponseText: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_provincie.field_provincie_tid_i18n' in 'field list': SELECT DISTINCT field_data_field_provincie.field_provincie_tid_i18n AS field_data_field_provincie_field_provincie_tid_i18n
FROM 
{node} node
LEFT JOIN {field_data_field_provincie} field_data_field_provincie ON node.nid = field_data_field_provincie.entity_id AND (field_data_field_provincie.entity_type = :views_join_condition_0 AND field_data_field_provincie.deleted = :views_join_condition_1)
WHERE (( (node.nid IN (:oids_0, :oids_1, :oids_2)) )); Array
(
[:views_join_condition_0] => node
[:views_join_condition_1] => 0
[:oids_0] => 358
[:oids_1] => 357
[:oids_2] => 332
)
in _views_filter_selective_query() (line 85 of /home/public_html/drupal/sites/countryholidaysholland.com/modules/views_hacks/views_filters_selective/views_filters_selective.module).

Looks like selective filtering is not working with translated terms (18n). Is there a way to make this work?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

metastate’s picture

I couldn't get Views Selective Exposed Filters to work with Views Translation either. Even though there is a filter option for "Content: Fieldname (field_fieldname) (translated) (selective)" it doesn't output anything in the View and I get the same errors as mentioned above. The SQL error when I load the view is line 81 for me:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ...
... in _views_filter_selective_query() (line 81 of .../sites/all/modules/views_hacks/views_filters_selective/views_filters_selective.module)

Internationalizations Views - https://drupal.org/project/i18nviews - is still in development though. And not sure about getting different filters to work together like this. Would be great though! But for now I have left the exposed filters in the default language so I can make use of the selective filter.

jonhattan’s picture

Status: Active » Needs review
FileSize
2.16 KB

I also hit this. Patched!

cmseasy’s picture

Thanks for the patch and the help to solve this issue.
With patch #2 the error in my view is not gone. Cleared cache: no effect. The exposed filter is a content field (node term reference). Do I need a relationship to a term? I tried that with no effect....

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /en/admin/structure/views/view/accommodaties/preview/page/ajax
StatusText: Service unavailable (with message)
ResponseText: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_provincie.field_provincie_tid_i18n' in 'field list': SELECT DISTINCT field_data_field_provincie.field_provincie_tid_i18n AS field_data_field_provincie_field_provincie_tid_i18n
FROM 
{node} node
LEFT JOIN {field_data_field_provincie} field_data_field_provincie ON node.nid = field_data_field_provincie.entity_id AND (field_data_field_provincie.entity_type = :views_join_condition_0 AND field_data_field_provincie.deleted = :views_join_condition_1)
WHERE (( (node.nid IN (:oids_0)) )); Array
(
[:views_join_condition_0] => node
[:views_join_condition_1] => 0
[:oids_0] => 366
)
in _views_filter_selective_query() (line 85 of /home/countryhh/domains/countryholidaysholland.com/public_html/drupal/sites/countryholidaysholland.com/modules/views_hacks/views_filters_selective/views_filters_selective.module).
jonhattan’s picture

FileSize
3.8 KB

Sorry I missed a file when generating previous patch. Check this one.

cmseasy’s picture

Hi, thanks for patch #4, it worked. The selective filters are correct with the selected languages and the correct languages are shown (translated taxonomy terms).

There are two items remaining.

When the selective filter gives an empty result in the filter, the label of the filter is viewable. A label with no filters has no function. The label should not shown as the selective filter is empty.

When selecting a selective/i18n filter item and hitting the submit button, all the selective/i18n items are gone and one button per label is leftover with the text 'Actual values will be available at run-time'. The view give an empty result. You cannot filter.
Filtering with a non-selective/i18n button gives a good result.

I will attache some images.

cmseasy’s picture

FileSize
463.71 KB
588.05 KB

The images

metastate’s picture

Thanks for the fix in #4. This gets the initial view to load properly - with both selective filter working and translations available. But when I submit an exposed filter, I get an empty view and each exposed filter now just says "Actual values will be available at run-time." (Same problem cmseasy mentions.)

Not sure if our configurations are different. This happens whether I have only one translated/selective filter added to the view or more than one. I am using both content and fields for my exposed filters:

Content: Fieldname1 (field_fieldname1) (translated) (selective)
Field: Fieldname2 (field_fieldname2) (translated) (selective)
Content: Fieldname3 (field_fieldname3) (translated) (selective)

anou’s picture

Hello,
Thanks for the patch but it doen't apply any more (I think because of lines break that aren't in original module file).

error: patch failed: views_filters_selective/views_filters_selective.module:70
error: views_filters_selective/views_filters_selective.module: patch does not apply

I had to do do it "a la mano" but I still got the error (it's in french but the error is the same ;-) ):

Une erreur HTTP AJAX s'est produite.
Code de statut HTTP : 500
Informations de débogage ci-dessous.
Chemin : /fr/admin/structure/views/view/event/preview/page/ajax
StatusText: Service unavailable (with message)
ResponseText : PDOException : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'taxonomy_term_data_field_data_field_thema.tid_i18n' in 'field list': SELECT DISTINCT taxonomy_term_data_field_data_field_thema.tid_i18n AS taxonomy_term_data_field_data_field_thema_tid_i18n
FROM 
{node} node
LEFT JOIN {field_data_field_thema} field_data_field_thema ON node.nid = field_data_field_thema.entity_id AND (field_data_field_thema.entity_type = :views_join_condition_0 AND field_data_field_thema.deleted = :views_join_condition_1)
LEFT JOIN {taxonomy_term_data} taxonomy_term_data_field_data_field_thema ON field_data_field_thema.field_thema_target_id = taxonomy_term_data_field_data_field_thema.tid
WHERE (( (node.nid IN (:oids_0, :oids_1, :oids_2, :oids_3, :oids_4, :oids_5, :oids_6)) )); Array
(
[:views_join_condition_0] => node
[:views_join_condition_1] => 0
[:oids_0] => 131
[:oids_1] => 369
[:oids_2] => 345
[:oids_3] => 343
[:oids_4] => 337
[:oids_5] => 298
[:oids_6] => 409
)
dans _views_filter_selective_query() (ligne 82 dans /home/[...]/sites/all/modules/views_hacks/views_filters_selective/views_filters_selective.module).

Just for info, as you can read in the error, I'm using a taxonomy term for the select (so it needs a relation)....

Thanks for any help on this.

cmseasy’s picture

I switched to Facet, following this workflow: http://beautiful7mind.wordpress.com/2013/03/10/step-by-step-how-to-imple...

Stil testing, but looks great: with facet you can create selective search (filtering) in an i18n environment. Maybe usefull for others.

Anonymous’s picture

Version: 7.x-1.0-alpha2 » 7.x-1.x-dev

~

Anonymous’s picture

Version: 7.x-1.x-dev » 7.x-1.0-alpha2

Hi
i patched #4 manually both to alpha2 and to dev version. results:
they work fine with regard to translation, that is translations show up properly, but the filter itself doesn't work anymore, that is filtered content is not found. in other words: exactley the same result as described in #7

Every idea to solve this is much apreciated!"

Regards

sunbam

anou’s picture

Version: 7.x-1.x-dev » 7.x-1.0-alpha2

Hello,
I didn't manage to resolve this issue but I found a way to get around it via a custom module and hook_form_alter(). You must use for example taxonomy term: term (selective) field for exposed filter and not the one with both "...(selective)(translated)". This way you get at least the options filtered by the view's results. Only not translated.

Then in your custom module:

<?php
/**
 * Implements hook_form_alter()
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  global $language;
  
  //exposed filter in my-view
  if ( $form_id == 'views_exposed_form' && is_array($form['system_name_of_my_field']) ) {

    $translated_term = array();

    foreach ( $form['system_name_of_my_field']['#options'] as $tid => $tname ) {
      //get term object
      $term = taxonomy_term_load($tid);
      //verify if term is really an object
      if( is_object($term) ) {
        //verify if term is translated. If not, leave it untranslated so we don't get an empty value.
        $translated_term[$tid] = isset($term->name_field['the_desired_language_code']) ? $term->name_field['the_desired_language_code'][0]['safe_value'] : $term->name_field['original_language_code'][0]['safe_value'];
      }
    }
    //$translated_term is not empty it means that "run time" has passed and select is populated.
    if ( !empty($translated_term)  && $language->language == 'the_desired_language_code' ) {
      $translated_term = array('All' => t('- All -')) + $translated_term;
      $form['system_name_of_my_field']['#options'] = $translated_term;
    }
  }
}
?>

This code is valid for terms translated via entity translation. You must use i18n_taxonomy_term_get_translation($term, $langcode) if you translate them via i18n and adapt the code.

skyredwang’s picture

Status: Needs review » Needs work

As mentioned above, the patch made the translation work, but broke the filter

jannis’s picture

The patch supplied in #4 works to stop the error message.

However -- the exposed filter still shows the UNTRANSLATED option.

AND

the filter breaks and returns nothing when selected. The view must be reset.

This is frustrating! Everything else i18n seems to work great.

Does anyone have a hack / patch to make this work ? A workaround? A suggestion?

anou’s picture

@jannis: have you tried the custom module like I propose in comment #12

Unfortunately I didn't find a way to solve the issue via a patch.

jannis’s picture

HERE'S A SOLUTION I'VE FOUND THAT WORKS TO TRANSLATE SELECTIVE FILTERS:

Important to note: i'm not using (translated) filters from i18n_views to do this:

I've modified one file: views_hacks\views_filters_selective\views_handler_filter_selective.inc

If you want exposed selective filters that will automatically translate the exposed values - here is code that will support it. A few notes: i'm using entity translation to translate individual fields. I'm using 'title' to turn node titles in translatable fields.

Fields that are 'select boxes' show up easier, if you're using fields (like 'title' for example) to filter content, you're going to have to name the field in the code (in my example i'm using if ($this->table == 'field_data_field_title_field') to send the array of titles as options for the filter.

Anyways -- here is the ENTIRE views_handler_filter_selective.inc file (if a more experienced Drupaler can create a patch or clean up this code, that'd be great!):

<?php

class views_handler_filter_selective extends views_handler_filter_in_operator {

static $results;
function get_value_options() {
$this->value_options = array(0 => t('Actual values will be available at run-time'));
if (empty($this->view->selective_oids) && !empty($this->view->inited)) {
$handler = _views_filters_selective_get_handler($this->definition['proxy']);
$oids = $this->get_oids();

//grabs the oids from the filter -- then grabs the titles
$newoptions = array();
foreach($oids as $key => $value){
$currentnode = node_load($value);
$newoptions[$key] = $currentnode->title;
}

//if this table is a title (or other non-check-box-field), define it here. the else portion will translate the check-box-fields
if ($this->table == 'field_data_title_field'){$options = $newoptions;}
else{$options = empty($oids) ? array() : call_user_func($handler, $this, $oids);}

if (!empty($options)) {

// Encode the values to keep special chars.
$this->value_options = array_combine(array_map('urlencode', array_values($options)), array_values($options));
}

// Modify value_options to include title for term node reference fields.
if ($this->definition['proxy'] == 'views_handler_filter_term_node_tid') {
foreach ($this->value_options as $tid) {
$result = db_query('SELECT name FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $tid));
$title = $result->fetchField();
if (!empty($title)) {
$this->value_options[$tid] = $title;
}
}
}

// Remove empty values.
$this->value_options = array_diff_key($this->value_options , array("" => NULL));

// Translate list options
$field = field_info_field($this->definition['field_name']);
foreach($this->value_options as $key => $value){
$options = array();
//$translate = i18n_field_type_info($field['type'], 'translate_options');
if (($translate = i18n_field_type_info($field['type'], 'translate_options')) && !empty($this->value_options)){
$this->value_options[$key] = $translate($field)[$value];}
else{

} }

// Re-sort results based on title.
asort($this->value_options);

}
}

function stringid($keys) {
return 'views:' . implode(':', $keys);
}

function value_form(&$form, &$form_state) {
parent::value_form($form, $form_state);
// Avoid the 'illegal values' Form API error.
$form['value']['#validated'] = TRUE;
}

function query() {
// Decode the values to restore special chars.
$this->value = array_map('urldecode', $this->value);
parent::query();
}

function get_oids() {
$exposed_input = isset($this->view->exposed_input) ? $this->view->exposed_input : array();
$signature = md5(serialize(array(
'name' => $this->view->name,
'args' => $this->view->args,
'input' => $exposed_input,
)));
if (empty(self::$results[$signature])) {
// Clone the view and get all results.
$view_copy = views_get_view($this->view->name);
if (!$view_copy) return NULL;
$view_copy->selective_oids = TRUE;
$view_copy->set_exposed_input($exposed_input);
$view_copy->set_arguments($this->view->args);
$view_copy->set_items_per_page(0);
if (isset($_GET['items_per_page'])) {
$items_per_page = $_GET['items_per_page'];
unset($_GET['items_per_page']);
}
$view_copy->execute($this->view->current_display);
if (isset($items_per_page)) {
$_GET['items_per_page'] = $items_per_page;
}
$oids = array();
foreach ($view_copy->result as $row) {
$oids[] = $row->{$view_copy->base_field};
}
if (empty($this->view->base_field)) {
$this->view->base_field = $view_copy->base_field;
}
self::$results[$signature] = $oids;
$view_copy->destroy();
}
return self::$results[$signature];
}
}

ggeerolf’s picture

Creating an extra module as mentioned in #14 did the trick.
I just replaced $translated_term[$tid] = isset($term->name_field['the_desired_language_code']) ? $term->name_field['the_desired_language_code'][0]['safe_value'] : $term->name_field['original_language_code'][0]['safe_value']; by $translated_term[$tid] = i18n_string('taxonomy:term:' . $term->name . ':name', $term->name);, where $term is my taxonomy-object.

georgemastro’s picture

This is what actually worked for me.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  global $language;

  // Translate the terms in selective filter.
  if ($form_id == 'views_exposed_form' && is_array($form['tid_selective'])) {
    $translated_term = array();
    foreach ($form['tid_selective']['#options'] as $tid => $tname) {
      // get term object
      $term = taxonomy_term_load($tid);
      // verify if term is really an object
      if (is_object($term)) {
        // verify if term is translated. If not, leave it untranslated so we don't get an empty value.
        $term_translated_obj = i18n_taxonomy_term_get_translation($term, 'en');
        $translated_term[$tid] = $term_translated_obj->name;
      }
    }
    // $translated_term is not empty it means that "run time" has passed and select is populated.
    if (!empty($translated_term)  && $language->language == 'en') {
      $translated_term = array('All' => t('- All -')) + $translated_term;
      $form['tid_selective']['#options'] = $translated_term;
    }
  }
}
infojunkie’s picture

Status: Needs work » Closed (won't fix)

This module has been deprecated in favour of https://drupal.org/project/views_selective_filters. Please test there and reopen if necessary.