--- /dev/null	2010-11-26 01:51:00.000000000 -0500
+++ mappers/nodereference.inc	2010-11-26 01:49:04.554200000 -0500
@@ -0,0 +1,91 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * On behalf implementation of Feeds API for mapping nodereference.module fields (CCK).
+ */
+
+/**
+ * Implementation of hook_feeds_node_processor_targets_alter().
+ *
+ * @see FeedsNodeProcessor::getMappingTargets()
+ */
+function nodereference_feeds_node_processor_targets_alter(&$targets, $content_type) {
+  $info = content_types($content_type);
+  if (isset($info['fields']) && is_array($info['fields'])) {
+    foreach ($info['fields'] as $field_name => $field) {
+      if ($field['type'] == 'nodereference') {
+        $field_label = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
+
+        $targets[$field_name . ':title'] = array(
+          'name' => $field_label . t(' (Node reference by node title)'),
+          'callback' => 'nodereference_feeds_set_target',
+          'description' => t('The CCK node reference !name of the node matched by node title.', array('!name' => $field_label)),
+          'real_target' => $field_name,
+        );
+
+        $targets[$field_name . ':nid'] = array(
+          'name' => $field_label . t(' (Node reference by node ID)'),
+          'callback' => 'nodereference_feeds_set_target',
+          'description' => t('The CCK node reference !name of the node matched by node ID.', array('!name' => $field_label)),
+          'real_target' => $field_name,
+        );
+      }
+    }
+  }
+}
+
+/**
+ * Implementation of hook_feeds_set_target().
+ *
+ * The actual field mapping happens in this callback function.
+ *
+ * @param obj $node
+ *   The node to to be created or updated.
+ * @param str $target
+ *   The name of the field on the node to be mapped.
+ * @param $value
+ *   The value(s) of the source feed item element to be mapped.
+ */
+function nodereference_feeds_set_target($node, $target, $value) {
+  // Determine whether we are matching against the title or nid.
+  list($target, $match_key) = explode(':', $target, 2);
+
+  // Allow for multiple-value fields.
+  $value = is_array($value) ? array_unique($value) : array($value);
+
+  $field = isset($node->$target) ? $node->$target : array();
+
+  // 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)) {
+          // Load field definition.
+          $field_info = content_fields($target, $node->type);
+          // Lookup potential exact matches for the value (limit to one result).
+          $matches = _nodereference_potential_references($field_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) && $v > 0) {
+          $nid = $v;
+        }
+        break;
+    }
+
+    if (isset($nid)) {
+      $field[] = array('nid' => $nid);
+    }
+  }
+
+  $node->$target = array_unique($field);
+}
