Index: mappers/references.inc
===================================================================
RCS file: mappers/references.inc
diff -N mappers/references.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ mappers/references.inc	4 Dec 2010 06:48:54 -0000
@@ -0,0 +1,168 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Mapper that handles any fields added by the References module (node_reference, user_reference)
+ *
+ * Code is effectively a port of the mapper_for_nodereference_field patch to drupal 7, and borrows
+ * heavily from the nodereference patch
+ *
+ */
+
+/**
+ * Implements hook_feeds_processor_targets_alter() for node_reference fields
+ *
+ * @see FeedsNodeProcessor::getMappingTargets().
+ *
+ */
+function node_reference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
+  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
+    $info = field_info_field($name);
+    if ($info['type'] == 'node_reference') {
+
+      $targets[$name . ':title'] = array(
+        'name' => $instance['label'] . t(' (Node reference by node title)'),
+        'callback' => 'references_feeds_set_target_node',
+        'description' => t('The @label field of the node matched by node title.', array('@label' => $instance['label'])),
+        'real_target' => $name);
+      $targets[$name . ':nid'] = array(
+        'name' => $instance['label'] . t(' (Node reference by node ID)'),
+        'callback' => 'references_feeds_set_target_node',
+        'description' => t('The @label field of the node matched by node ID.', array('@label' => $instance['label'])),
+        'real_target' => $name);
+    }
+  }
+}
+/**
+ * Implements hook_feeds_processor_targets_alter() for user_reference fields
+ *
+ */
+function user_reference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
+  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
+    $info = field_info_field($name);
+    if ($info['type'] == 'user_reference') {
+
+      $targets[$name . ':name'] = array(
+        'name' => $instance['label'] . t(' (User reference by user name)'),
+        'callback' => 'references_feeds_set_target_user',
+        'description' => t('The @label field of the user matched by user name.', array('@label' => $instance['label'])),
+        'real_target' => $name);
+      $targets[$name . ':uid'] = array(
+        'name' => $instance['label'] . t(' (User reference by user ID)'),
+        'callback' => 'references_feeds_set_target_user',
+        'description' => t('The @label field of the user matched by user ID.', array('@label' => $instance['label'])),
+        'real_target' => $name);
+    }
+  }
+}
+
+/**
+ * Callback for mapping node reference fields
+ *
+ */
+function references_feeds_set_target_node($source, $entity, $target, $value) {
+  if (empty($value)) {
+    return;
+  }
+
+  // Handle non-multiple values.
+  if (!is_array($value)) {
+    $value = array($value);
+  }
+
+  list($target, $match_key) = explode(':', $target, 2);
+
+  $i = 0;
+  $field = isset($entity->$target) ? $entity->$target : array();
+  $info = field_info_field($target);
+
+  // Match values against nodes and add to field.
+  foreach ($value as $v) {
+    $nid = NULL;
+
+    // Is it a good idea to silently fail if the value isn't a valid nodereference?
+    switch ($match_key) {
+      case 'title':
+        // Validate title.
+        if ((is_string($v) && $v != '') || is_numeric($v)) {
+          // Lookup potential exact matches for the value (limit to one result).
+          $matches = _node_reference_potential_references($info, $v, 'equals', array(), 1);
+          // Use the first element of the potential matches.
+          $nid = key($matches);
+        }
+        break;
+
+      case 'nid':
+        // Make sure it is a positive integer.
+        if ((is_int($v) || ctype_digit($v)) && $v > 0) {
+          $nid = (int) $v;
+        }
+        break;
+    }
+
+    if (isset($nid)) {
+      $field['und'][$i]['nid'] = $nid;
+    }
+    if ($info['cardinality'] == 1) {
+      break;
+    }
+    $i++;
+  }
+  $entity->{$target} = $field;
+}
+
+/**
+ * Callback for mapping user reference fields
+ *
+ */
+function references_feeds_set_target_user($source, $entity, $target, $value) {
+  if (empty($value)) {
+    return;
+  }
+
+  // Handle non-multiple values.
+  if (!is_array($value)) {
+    $value = array($value);
+  }
+
+  list($target, $match_key) = explode(':', $target, 2);
+
+  $i = 0;
+  $field = isset($entity->$target) ? $entity->$target : array();
+  $info = field_info_field($target);
+
+  // Match values against users and add to field.
+  foreach ($value as $v) {
+    $uid = NULL;
+
+    // Is it a good idea to silently fail if the value isn't a valid userreference?
+    switch ($match_key) {
+      case 'name':
+        // Validate name.
+        if ((is_string($v) && $v != '') || is_numeric($v)) {
+          // Lookup potential exact matches for the value (limit to one result).
+          $matches = _user_reference_potential_references($info, $v, 'equals', array(), 1);
+          // Use the first element of the potential matches.
+          $uid = key($matches);
+        }
+        break;
+
+      case 'uid':
+        // Make sure it is a positive integer.
+        if ((is_int($v) || ctype_digit($v)) && $v > 0) {
+          $uid = (int) $v;
+        }
+        break;
+    }
+
+    if (isset($uid)) {
+      $field['und'][$i]['uid'] = $uid;
+    }
+    if ($info['cardinality'] == 1) {
+      break;
+    }
+    $i++;
+  }
+  $entity->{$target} = $field;
+}
