7

I have a content type with multilingual support (node translation). This content type also has an entity reference (module: entityreference) field.

My content is:

/node/1 English story 1
/node/2 French  story 1 (translation of #1)
/node/3 English story 2 with reference to #1
/node/4 French  story 2 (translation of #3) **entityreference problem here**

I've configured the reference to be synchronized. The problem is that whenever I translate or edit a node, it'll try to sync the reference to other translations.

So I'll always end up with:

#3 -> #1 (correct)
#4 -> #1 (wrong)

or:

#3 -> #2 (wrong)
#4 -> #2 (correct)

I can solve this by disabling syncing on the reference field but then I will have to be maintaining the references for all translations manually which is a problem in a website with many translations.

4 Answers 4

3

You should try module "Synchronize entity references" (i18n_entityreference) from project Internationalization contributions (i18n_contrib). So far, for me it looks perfect. It even works with inline entity form widgets.

To install and use it:

  1. Install and enable the module. If you use drush:

    drush dl i18n_contrib && drush en i18n_entityreference
    

    (This requires the core module i18n_sync and will enable it automatically if not already done. It is needed for synchronizing field values across translations.)

  2. Configure "Synchronize translations". Basically go to "Structure → Content Types → [your content type]" in the admin menu, switch to tab "Synchronize translations", select your node reference field, and save. See here for details.

  3. Use. If you have existing content, the node reference field will be synced in a language-aware way when you save any node of a translation set for the next time.

1
3

Had the same problem, wrote this little workaround:

function YOURMODULE_i18n_sync_translation($entity_type, $translation, $translation_language, $source, $source_language, $field_names) {
  foreach ($field_names as $field_name) {
    $info = field_info_field($field_name);
    if ($info['type'] == 'entityreference') {
      $items = field_get_items($entity_type, $source, $field_name);
      foreach ($items as $delta => $item) {
        $translations = translation_node_get_translations($item['target_id']);
        if (isset($translations[$translation_language])) {
          list($id, $vid, $bundle) = entity_extract_ids($entity_type, (object) $translations[$translation_language]);
          $translation->{$field_name}['und'][$delta]['target_id'] = $id;
        }
      }
    }
  }
1
  • Note that it works well when changing the source node.
    – cherouvim
    Nov 27, 2014 at 13:40
2

There's a module for that: https://drupal.org/project/i18n_node_reference

Provides i18n field synchronisation for a node_reference field.

The referenced nodes are translated into the correct language. If no such language exists, it is ignored. This is the same functionality as is supported by the taxonomy reference translation in i18n_taxonomy.

1
  • 1
    I use the entityreference and not references module. So this doesn't apply in my case.
    – cherouvim
    Nov 26, 2014 at 17:03
2

The code snippet in the second answer won't work when editing the reference in a translation. That's because translation_node_get_translations() accepts a tnid as argument, not a nid. Check the api docs.

Change the call in the above code to something like this and it should work (for nodes only, in this case):

$referenced_node = node_load($item['target_id']);
$translations = translation_node_get_translations($referenced_node->tnid);

NOTE: I couldn't comment on the other answer, so i'm posting this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.