With the normal translation there is a filter that checks the languages of the node. So you could for example only display the nodes that are in the users language.
With Entity translation this filter doesn't work because the node is in 1 language en the field are translated. The problem now is that if a node is published but no translation is provide a message will be displayed that the node is not translated or it will show the node but only the fields that can't be translated.

By turning on 'Enable language fallback' the node will be displayed in another language but this is an overall setting and maybe you just want this for certain node types or views.

So a filter that can be set per view would be more flexible. Maybe something that checks if a translation for the node is added?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GiorgosK’s picture

GiorgosK’s picture

Title: Views language filter » ET: Views field language filter

here is my use case which needs a field

content type entity translated
with title_field and image field both entity translatable
view using content:fields

if my view gets nodes which all have translations the view works great using no filter and settings "Query settings:Settings Distinct" and "Field Language:Current user's language" and on the field settings I check "hide empty fields"

when one or more nodes don't have translation the view gets messud up if I add a field which is never empty for all language like NID, Edit Link etc

using a filter like "title_field NOT EMPTY" or "image field NOT EMPTY" does not work either and views shows all nodes, some nodes might have only NID or Edit link displayed since there is no real translation of the tile or image ...

thus a views field language filter would be most useful in such cases

a temporary workaround would be to user hook_views_query_alter(&$view, &$query) OR hook_views_pre_render(&$view) or user Views PHP for filtering the output

danielnolde’s picture

fyi: Filter views by ET translation language

I'm planning to implement (and contribute via patch) a views filter that filters the selection of entities in views for selecting only entities that have a original version or translation in the current display language.

Please give your input at http://drupal.org/node/1402692

danielnolde’s picture

A sandbox module for filtering content queries for Entity Translation content by proper language has been created, see http://drupal.org/node/1402692#comment-5468944

Kristen Pol’s picture

Title: ET: Views field language filter » Entity translation: Views field language filter
Jax’s picture

Status: Active » Needs work
FileSize
1.46 KB

I was rather thinking about something like what I've attached. This filters on the language field of the entity_translation table. So as long as you haven't created a translation it doesn't exist in this table and you can filter on it.

Currently the patch only works with the node table but see for example field.views.inc to make it more generic to work with all entities. Will continue to work on this.

danielnolde’s picture

That's cool, too, Jax. I think _both_ your filter in #5, using views' filter alteration, and my filter in #4, using query rewrite (configurably broadened to any query in Drupal) should be included into ET. And both filters need some work to cover non-node entities, too. Perhaps we can join forces, and perhaps the ET maintainer can provide some opinion on this and how/in what form to get it into ET (blink-blink towards Daniel ;)

plach’s picture

Hi Daniel, I'm definitely in favor of having built-in Views support, unfortunately I could not follow your progress yet. Would you mind to summarize the current status in the other issue, if anything changed from http://drupal.org/node/1402692#comment-5468944?

Obviously we need to support any entity.

BarisW’s picture

Jax, any updates on this? Would love to be able to create a view that shows me all files (images) that are untranslated. Thanks!

Jax’s picture

@BarisW The patch provided in #6 works so I'm not sure what you want.

BarisW’s picture

@Jax:

"Currently the patch only works with the node table but see for example field.views.inc to make it more generic to work with all entities. Will continue to work on this."

I need it to filter images (files), not nodes. That's why I was wondering if you got any further (as you mentioned you were continue working on it)?

Jax’s picture

Oh, I guess I did say that. I'll try to do that then in the next couple of days.

BarisW’s picture

Cool, thanks!

khaled.zaidan’s picture

Great, this seems to work... would love to see it committed!

plach’s picture

I think this particular use case should be solved by Views, since it's basically fully supporting the core Field language API. What @danielnolde linked in #3 is a different issue that belongs to ET.

See #1515156: Expose the field language column for translatable fields.

plach’s picture

plach’s picture

Status: Closed (works as designed) » Postponed

Actually postponing to see if the patch linked above is accepted in Views.

Jax’s picture

Component: Code » Base system
FileSize
1.45 KB

I've updated the patch to support all the entity types since I said I would do that. Is completely untested.

stevieb’s picture

FileSize
188.58 KB
34.14 KB

I tried to using the patch to show a glossary of node types with a thumbnail, title and subtitle in both a grid & table
.. using all choices in "Entity translation: Language" (users language, german or english) ... sadly it failed and everything is still doubled
see img

plach’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.x-dev
Component: Base system » Views integration
cossimo’s picture

FileSize
18.68 KB

Just to follow up on what stevieb wrote in #19, my experience was the same. The patch does expose the entity_translation table as Views filters; however, this doesn't help with filtering out duplicates that result when the fields of a node are translated. From my experience, when a node has X translatable fields, and the node has been translated into Y languages, XY duplicate results will appear for that node. (This is the case regardless of whether Language Fallback is enabled or not.) Setting the "Query settings" to "Distinct" will prune the results down some; however, at best, I get a duplicate result for every translated field in the node.

This particular issue is different from the original issue in this thread, which focuses more on gracefully handling untranslated nodes and fields, and as such should probably be submitted as its own issue. However, it seems so fundamental, that I would just like to make sure I'm not missing something before I submit it as its own issue.

That is, is there a tool, filter, or strategy I'm missing that will allow me to filter a result set in such a way that multiple translated fields associated with a single node do not result in duplicate results for that node? And if there is no way to filter results in this manner, where should this issue be submitted? Under the Views project or the Entity Translation project?

I have attached an export of the view. The version of Entity Translation I am using is 7.x-1.0-alpha2 (6-16-12), and the version of Views I am using is 7.x-3.3+173-dev (6-17-12).

Thanks very much for your consideration and help with this!

Chris

torpy’s picture

Yea, I had that same problem. The following piece of code may not be the best way to do things but seems to solve it for me. This would go in a hook_views_query_alter() function. It's hardcoded to only work for views that display one type of node but can be modified to work for multiple node types.

if ($view->base_table == 'node') {
  if (!empty($view->filter['type']) && count($view->filter['type']->value) == 1) {
    $node_type = reset($view->filter['type']->value);
    $translation_type = variable_get('language_content_type_' . $node_type, 0);

    if ($translation_type == ENTITY_TRANSLATION_ENABLED) {
      global $language;
      $languages = array(LANGUAGE_NONE, $language->language);
      $language_join = array(
        'field' => 'language',
        'value' => $languages,
      );

      foreach ($query->table_queue as $table_name => $table) {
        if (strpos($table_name, 'field_data') !== FALSE) {
          $query->table_queue[$table_name]['join']->extra[] = $language_join;
        }
      }
    }
  }
}

The issue seems to be that views sometimes uses entity_load to get field definitions, but other times just JOINs the field table directly on and retrieves data that way.

creando sensaciones’s picture

subscribe

BarisW’s picture

@creando sensaciones: Stop subscribing, start following

cossimo’s picture

@torpy, re: #22, from what I've seen so far, this works great! Thanks so much!

C.

Yuri’s picture

Do i undestand right that #22 is the best solution so far, for filtering views fields by entity language?
If so, then I don't know how to implement the code provided in #22, sorry.
A patch or steps to get this working would be great, thanks!

heretic381’s picture

Same here. I've tried with template.php, but cant get this to work.

cossimo’s picture

You will need to put the function in a custom module. I can post mine for you tomorrow for reference.

C.

heretic381’s picture

Ok. That would be helpfull I think.

cossimo’s picture

FileSize
688 bytes

Very sorry... This slipped my mind. Attached is my custom module. (It may not meet Drupal coding standards, but it works.)

C.

Yuri’s picture

Just to be clear about the main topic of this issue: Entity translation: Views field language filter.
I use entity translation on groups (which seems the only way for OG to translate groups)
My issue is that I want to filter groups in views, by current user language.
When I select Content: Language (in Current user's language) the view doesnt show any results, doesn't show the groups that are translated.

As for the module mentioned in #30, I installed it but have no idea what i should see or do, i don't see any change in settings or views output.

Can someone please assist me with this issue, it seems that this thread is the only one that handles views language filter for entity translations.

cossimo’s picture

Yuri:

Per recent activity addressing this problem in Views, the following patch is a better route to take for resolving this issue: https://drupal.org/node/1515156#comment-6511750

After applying the patch, the language(s) for any translatable field can be used as filter criteria for the view. It works very well, though in some instances, I've also had to check "Distinct" under Query settings.

C.

plach’s picture

Status: Postponed » Closed (duplicate)

Yes, that patch over there has been committed to the Views project so we don''t need this anymore.

calculus’s picture

Is there a patch/fix for #21 problem, other than #22 manual override?

plach’s picture

The fix is in the Views issue linked above: you can specify a filter for a particular language as you would do for the field delta.

calculus’s picture

Thank you.

Trias2013’s picture

What worked for me:

  1. Under "Advanced", add a relationship with "Content: Entity translation: translations"
  2. Under "FILTER CRITERIA", add a filter: "(Translations) Entity translation: Language (= Current user's language)"
  3. => No custom patching needed

Greetings

justintorrent’s picture

not work for me!

bdecarne’s picture

Thanx, #37 worked like a charm.

jrochate’s picture

I didn't even need the Advanced Relationship.

Just add the filter criteria on entity translatable item, and voila.

Thanks for the heads-up

schifazl’s picture

#37 worked for me too. thanks!

SunnyGambino’s picture

Thanx, #37 worked like a charm. Cool solution! :)

jetwodru’s picture

why #37 does NOT work for me ? I only have a translatable item (position) under Sort Criteria and all are screwed up. Following #37 sitll does not work, any other solutions ? Without this being solved, Entity Translation has become useless to me but I really love its working concept. Thanks

plach’s picture

You should find a "position:language" item in the filters section: add it, select the type of language filtering and you should be done.

jetwodru’s picture

#44, thanks, it works now even without adding any Relationship but the Language Fall Back for the Views do NOT work. I have 5 languages enabled but not all contents are translated for each language and English being the default language all the times. I wanted those contents with no translations will always fall back to English instead of showing empty block.

P/S: I'm quite confused. It seems various ways of working around with Views. Some people can get it to work with only Entity Translation:Language and some require adding Relationship. However, I have to select the Translatable Field Language in order to work. I can't really come to a conclusion of how Entity Translation works with Views in different contexts. I wish I would be enlightened on its usage. Thanks.

danielbehina’s picture

Issue summary: View changes

I tried all and nothing works unless I have found simple soulution here:

If you have a Sort Criteria on the title_field you may have your nodes appear twice. To fix this, you just need to add a Filter Criteria on the title_field:language (Current user's language) and it works !

ET is the best and I can't live without it, thank you for your work! Hope it helps.

clairedesbois@gmail.com’s picture

FileSize
577 bytes

I have make a patch for the last version of dev. Just add filters on entity translation informations.

liquidcms’s picture

Status: Closed (duplicate) » Active

just to muddy the waters even more... i am trying to filter by contextual filter. this issue seems to refer to only filters (and i have tried adding relationship and contextual filter for language and this does not work).

but i see plach also closed this issue: #1305770: Entity Translation shows all translation if using a contextual filter (which is about contextual filters) stating it was a duplicate of this issue.

so, does this also work for contextual filters? any hints as to setup?

just to be clear that i have same issue:

- i have a json output from a view which is set to "Field language: Current user's language" (not sure how this doesn't interfere with any filter settings??).
- the path to my page is json/data
- if i go to en/json/data i get english data
- if i go to da/json/data i get danish data
YAY.. perfect!!

but now, i have a cron task which calls views_embed_view() to pull data in each language (to calculate a hash on the data).. and i need to pass an arg for language to views_embed_view() to get the correct data.

sorry to re-open but this is closed as duplicate; when others are closed as duplicate stating this is the original (and nothing listed here saying what this is the duplicate of).. getting very confusing..

liquidcms’s picture

thought i would be smart and simply set global language within my cron language loop.. but oddly that has no impact (always returns EN data).

feldmarv’s picture

GREAT THANKS to, #37! This works great, after searching for hours.

Renrhaf’s picture

#37 works great, thank you.

kenorb’s picture

Status: Active » Needs review

The last submitted patch, 6: views-filter.patch, failed testing.

The last submitted patch, 18: entity_translation.patch, failed testing.

samit.310@gmail.com’s picture

Hi All,

Following Code works For me.

  if ($view->base_table == 'node' && !empty($view->filter['type'])) {
    global $language;
    $languages = array($language->language);
    $language_join = array(
      'field' => 'language',
      'value' => $languages,
    );
    foreach ($query->table_queue as $table_name => $table) {
      if (strpos($table_name, 'field_data_field') !== FALSE) {
        $query->table_queue[$table_name]['join']->extra[] = $language_join;
        $query->table_queue[$table_name]['join']->definition['extra'][] = $language_join;
      }
    }
  }
parameshrag’s picture

#37 works great, it saved my time, thank you.

knalstaaf’s picture

#37 may work for the overall approach, but it may be insufficient for views using a contextual filter (such as NID), as mentionned in #48. For the latter a solution my be provided in #1346242-21: Duplicates in views using entity translation when sorting results (regardless of whether sorting criteria are being used or not).

It may also be fixed by simply adding the very same field as a filter, but as the language property of it (e.g.: field_images:language)

ezoulou’s picture

+1 for clean workarround #37 :-)

GoZ’s picture

#37 does not work for me. I can't find filter.

Otherwise, #47 works great. It's add every filters i need and query looks great

4kant’s picture

Thanks to #46 - no patch necessary ;-)

cosolom’s picture

#46 work's fine. thanks

plach’s picture

Status: Needs review » Closed (duplicate)

Please don't reopen closed issues, #47 is about making all ET info available to Views as fields, filters and sorts and therefore is a new feature request. Additionally it works only for nodes so it cannot be committed in the present form. Feel free to open a new issue and link it here because this has become totally unmanageable.

alsator’s picture

This works for me:

function MODULE_NAME_views_query_alter(&$view, &$query) {
	
	if($view->name == 'VIEWS_NAME') {
		
		$join = new views_join();
		$join->table = 'entity_translation';
		$join->field = 'entity_id';
		$join->left_table = 'node';
		$join->left_field = 'nid';
		$join->type = 'inner';
		$query->add_relationship('entity_translation', $join, 'node');
		
		global $language;
		
		$query->where[1]['conditions'] = array(
			array(
				'field' => 'entity_translation.language',
				'value' => $language->language,
				'opperator' => '='
			),
		);
	}
}