--- node_reference/node_reference.module.old 2011-07-16 00:28:27.000000000 +0200 +++ node_reference/node_reference.module 2011-07-16 00:26:28.000000000 +0200 @@ -870,33 +870,42 @@ * * When preparing a translation, load any translations of existing * references. - * @todo Correctly implement after http://drupal.org/node/362021 is fixed. */ -function node_reference_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items) { - $addition = array(); - $addition[$field['field_name']] = array(); +function node_reference_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { + $source_language = $entity->translation_source->language; 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_node = node_load($reference['nid']); - // 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_node->type) - && !empty($reference_node->language) - && $reference_node->language != $node->language - && $translations = translation_node_get_translations($reference_node->tnid)) { - // If there is a translation for the current language, use it. - $addition[$field['field_name']][] = array( - 'nid' => isset($translations[$node->language]) - ? $translations[$node->language]->nid - : $reference['nid'], - ); - } - } - } + $references = $entity->translation_source->$field['field_name']; + if (key_exists($source_language, $references)) { + foreach ($references[$source_language] as $key => $reference) { + $options = array('ids' => array()); + $reference_node = node_load($reference['nid']); + // 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_node->type) + && !empty($reference_node->language) + && $reference_node->language != $entity->language + && $translations = translation_node_get_translations($reference_node->tnid)) { + // If there is a translation for the current language store in validation array + if(isset($translations[$entity->language])) { + $options['ids'][] = $translations[$entity->language]->nid; + } + } + + // add original node to validate + $options['ids'][] = $reference['nid']; - return $addition; + // filter original content and translated content + $refs = node_reference_potential_references($field, $options); + $options['ids'] = array_intersect($options['ids'], array_keys($refs)); + + // gets first element of filtered array (translated first, then original). + // if not sets FALSE + $items[$key] = array('nid' => reset($options['ids'])); + } + } + } } /**