Hi there,

I have a similar problem as in #1421248: Trouble referring to translated nodes. I have two (node) content types "event" and "location" where "event" has a entityreference field to add a location to that event. The site is currently bilingual; all events and each location node is translated in both languages. If I now create a new event and want to add a location via autocomplete (single) the list contains each location twice (one for each language). [Well, most of them since location's name doesn't differ in different languages often.]

If I create an English event though, I would like to only get a list of all English location nodes. That's exactly what my patch now does for me. Only file changed is handler/base.inc and it includes an option to control that behaviour through content type > manage fields > entity reference setting (below the sorting options).

Might come in handy for other users maintaining bi/multilingual sites. Might even be a start to make entityreference i18n aware?

Cheers,
Paul

Comments

Status: Needs review » Needs work

The last submitted patch, base.inc_.patch, failed testing.

broon’s picture

StatusFileSize
new1.16 KB

Cleaned up patch file.

broon’s picture

Status: Needs work » Needs review

base.inc_.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, base.inc_.patch, failed testing.

broon’s picture

Status: Needs work » Active

Could someone please review my patch and point me in the right direction why it keeps failing? The report always states the last line as an issue. Also there's no MySQL involved, so I am a bit puzzled.

broon’s picture

One more try. Doesn't seem like this is needed by anybody anyways. ;)

tiagojsag’s picture

+1 for this fix

tiagojsag’s picture

So I was trying this patch, and for me it doesn't work out of the box. For it to work for me, I had to move the buildEntityFieldQuery part to before the if(isset($match)) part, like so:

protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', $this->field['settings']['target_type']);
    if (!empty($this->field['settings']['handler_settings']['target_bundles'])) {
      $query->entityCondition('bundle', $this->field['settings']['handler_settings']['target_bundles'], 'IN');
    }
    $entity_info = entity_get_info($this->field['settings']['target_type']);
    if (module_exists('i18n') && $this->field['settings']['handler_settings']['filter']['language'] == 1 && $entity_info['translation']['locale'] == TRUE) {
    	global $language;
    	$query->propertyCondition('language', $language->language);
    }
    if (isset($match)) {
      $entity_info = entity_get_info($this->field['settings']['target_type']);
      if (isset($entity_info['entity keys']['label'])) {
        $query->propertyCondition($entity_info['entity keys']['label'], $match, $match_operator);
      }
    }

I was looking around but couldn't figure out what the $match variable stands for exactly.

tiagojsag’s picture

Also, the commited patch filters by the users current session. Using your use case, should you be editing a content in language A but your session/user/whatever is set to language B, you will end up associating an event translated to A with a location translated to B.

In my particular case, I need to filter by the parent node language instead of the user language, so I made the following aditional changes (based on my previous post)

protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', $this->field['settings']['target_type']);
    if (!empty($this->field['settings']['handler_settings']['target_bundles'])) {
      $query->entityCondition('bundle', $this->field['settings']['handler_settings']['target_bundles'], 'IN');
    }
    $entity_info = entity_get_info($this->field['settings']['target_type']);
    if (module_exists('i18n') && $this->field['settings']['handler_settings']['filter']['language'] == 1 && $entity_info['translation']['locale'] == TRUE) {
    	$node = menu_get_object();
    	$query->propertyCondition('language', $node->language);
    }
    if (isset($match)) {
      $entity_info = entity_get_info($this->field['settings']['target_type']);
      if (isset($entity_info['entity keys']['label'])) {
        $query->propertyCondition($entity_info['entity keys']['label'], $match, $match_operator);
      }
    }

Since I'm a Drupal noob, I ask that someone take a look at this to see if I made any stupid mistake.

broon’s picture

Hey tiagojsag,

thanks for your input. I applied your changes, but had to alter them. If you are creating a new node it does not have language set yet. For this case I made a fallback using global language.
Anyways, I also figured out why the patches might not have worked: my code base was still beta3 and folder structure and file names have been changed through beta5. So here is my new patch against 7.x-1.0-beta5.

broon’s picture

Status: Active » Needs review

Testing new patch (hopefully).

Status: Needs review » Needs work

The last submitted patch, entityreference-language_filter-1462766-10.patch, failed testing.

broon’s picture

Status: Needs work » Needs review
StatusFileSize
new2.08 KB

Another shot at automated testing (changed line endings).

Status: Needs review » Needs work

The last submitted patch, entityreference-language_filter-1462766-12.patch, failed testing.

broon’s picture

Status: Needs work » Needs review
StatusFileSize
new2.01 KB

And another try (adjusted paths).

Status: Needs review » Needs work

The last submitted patch, entityreference-language_filter-1462766-14.patch, failed testing.

broon’s picture

Status: Needs work » Needs review
StatusFileSize
new2.02 KB

I am sorry to spam this thread with my various testings, but I really like to know how to get this automated system to work with such a simple patch.

Status: Needs review » Needs work

The last submitted patch, entityreference-language_filter-1462766-17.patch, failed testing.

damien tournoud’s picture

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

Hi there, thanks for the patch, but we are not going to support this.

If the list of entities you are referencing is different between languages, you should make that field translatable, and use an UI (for example the one provided by Entity Translation) to edit the different language versions.

joel_osc’s picture

I really like the functionality that this patch proposes and think that this feature should be considered for this module, even if field translations are used as stated above because even with a translated field the autocomplete list has all your nodes of every language which has issues from a usability perspective.

That being said I have been testing this patch and have found there are still a few issues - kudos for the effort though! I have a bilingual site with nodes which are translated using node translations and I also have a localized (not translated) taxonomy vocabulary. Both the nodes and the taxonomy terms have a field collection with an entity reference field, the field collection for nodes is not translatable but the field collection for terms is translatable. And in both cases the reference field is not translatable because it makes no sense to do that for a field collection.

Using the patch above, the following cases are problematic or not covered:

1. when adding a node translation the url looks like this: eng/node/add/page?translation=23&target=fr which means that $node = menu_get_object(); returns null and the ER field defaults to English when the user needs to add French references. The translated node has to be saved and then re-edited to add French node references.

2. Taxonomy term references are always shown in the user's current language for the same reason as above - $node = menu_get_object(); returns null. The user then has no way of adding references to French content.

I don't think there is an easy answer for the situation that I have outlined above, but I do believe that further dialogue is required on how an ER field should behave in a multi-lingual environment. Cheers.

joel_osc’s picture

Here is a slight modification to the patch to use i18n_node_i18n_context_language() to find the corrent language context, which addresses #1 from my comment above. Also, for anyone else that runs into it, you need to save each entityreference field on your content type after adding this patch or you will get an error Undefined index: filter in EntityReference_SelectionHandler_Generic line 235.

amir simantov’s picture

I might be missing the point of this issue but what does this patch provide that is not provided with the Multilingual select module which comes with the i18n project?

I actually found this issue when looking for a solution to another, yet related, problem; I wanted that once a referenced item in language X is referenced by a given X language the next will happen: when translating the referencing item to language Y there will be no need to reference manually to the translation of the referenced item, that is, to an item of language Y, it will be done automatically.

broon’s picture

@Amir: That's actually the big picture.

Our current setup is having different content types (A and B), both are multilingual with translations. B contains an entity reference field to A. We are using node translation (opposed to field translation) because we have different workflows (using workbench moderation) for each language.

Now, an editor creates node of type A and translates it (nid 1{en} & 2{de}). Then, the editor created node of type B (nid 3, en) and adds the reference to node 1. Finally, when the editor adds the translation for nid 3, the entity reference field is pre-populated with a reference to nid 1 (which should be 2, though). In this case, the reference has to be entered again.

Since we do not use field translation (see above) and we only want to reference node of same language, we had to make the autocomplete function language aware. This issue, however, is fixed for us even though it will apparently not be included in this module. The other issue is discussed in various threads, e.g.

#1421248: Trouble referring to translated nodes
#1332262: The referenced entity is invalid (for translations)

anou’s picture

This is maybe why it kept failing testing:

entityreference-language_filter-1462766-21.patch:11: space before tab in indent.
    	$form['filter']['language'] = array(
entityreference-language_filter-1462766-21.patch:12: space before tab in indent.
    			'#type' => 'select',
entityreference-language_filter-1462766-21.patch:13: space before tab in indent.
    			'#title' => t('Language handling'),
entityreference-language_filter-1462766-21.patch:14: space before tab in indent.
    			'#options' => array(
entityreference-language_filter-1462766-21.patch:15: space before tab in indent.
    					0 => t('Select all nodes'),
warning: squelched 4 whitespace errors
warning: 9 lines add whitespace errors.
netsensei’s picture

Rather then patching Entity Reference itself, I've implemented the code in #21 into it's own module. Entity Reference conveniently uses the CTools Plugin API thus making the widgets extensible as plugins.

A solution which also adheres to the remark by Damz in #19.

Module is called Translated Entity Reference (for lack of a better name)

See: https://drupal.org/sandbox/netsensei/2168701

vacilando’s picture

Thanks, @netsensei: the module (now called https://drupal.org/project/translated_entityreference) is a perfect solution for this problem!