You are here

function field_reference_field_prepare_translation in Field reference 7

Implements hook_field_prepare_translation().

When preparing a translation, load any translations of existing references.

File

./field_reference.module, line 997
Defines a field type for referencing a field from another.

Code

function field_reference_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $addition = array();
  $addition[$field['field_name']] = array();
  if (isset($entity->translation_source->{$field}['field_name']) && is_array($entity->translation_source->{$field}['field_name'])) {
    foreach ($entity->translation_source->{$field}['field_name'] as $key => $reference) {
      $reference_entity = entity_load_single($reference['id']);

      // Test if the referenced node type is translatable and, if so,
      // load translations if the reference is not for the current language.
      // We can assume the translation module is present because it invokes 'prepare translation'.
      if (translation_supported_type($reference_entity->type) && !empty($reference_entity->language) && $reference_entity->language != $entity->language && ($translations = translation_node_get_translations($reference_entity->tnid))) {

        // If there is a translation for the current language, use it.
        $addition[$field['field_name']][] = array(
          'nid' => isset($translations[$entity->language]) ? $translations[$entity->language]->nid : $reference['nid'],
        );
      }
    }
  }
  return $addition;
}