? link_mapper_upgrade.patch
Index: mappers/link.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/feeds/mappers/link.inc,v
retrieving revision 1.5
diff -u -p -r1.5 link.inc
--- mappers/link.inc	17 Sep 2010 17:25:19 -0000	1.5
+++ mappers/link.inc	18 Oct 2010 15:03:29 -0000
@@ -1,87 +1,81 @@
 <?php
-// $Id: link.inc,v 1.5 2010/09/17 17:25:19 alexb Exp $
 
 /**
- * @file
- * On behalf implementation of Feeds mapping API for link.module (CCK).
- */
-
-/**
- * Implements hook_feeds_node_processor_targets_alter().
+ * Implements hook_feeds_processor_targets_alter().
+ *
+ * @see FeedsNodeProcessor::getMappingTargets().
  */
-function link_feeds_node_processor_targets_alter(&$targets, $content_type) {
-  $info = content_types($content_type);
+function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
+  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
 
-  $fields = array();
-  if (isset($info['fields']) && count($info['fields'])) {
-    foreach ($info['fields'] as $field_name => $field) {
-
-      if (in_array($field['type'], array('link'))) {
-        $name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
-        $targets[$field_name .':url'] = array(
-          'name' => t('!field_name (URL)', array('!field_name' => $name)),
+    $info = field_info_field($name);
+    $allowed_types = array(
+      'link_field',
+    );
+
+    if (in_array($info['type'], $allowed_types)) {
+      if (array_key_exists('url', $info['columns'])) {
+        $targets[$name . ':url'] = array(
+          'name' => $instance['label'] . ' URL',
           'callback' => 'link_feeds_set_target',
-          'description' => t('The URL for the CCK !name field of the node.', array('!name' => $name)),
-          'real_target' => $field_name,
+          'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
+        );
+      }
+      if (array_key_exists('title', $info['columns'])) {
+        $targets[$name . ':title'] = array(
+          'name' => $instance['label'] . ' Title',
+          'callback' => 'link_feeds_set_target',
+          'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
         );
-
-        //Provides a mapping target for the field title if used.
-        if (in_array($field['title'], array('optional', 'required'))) {
-          $targets[$field_name .':title'] = array(
-            'name' => $name .' (' . t('title').')',
-            'callback' => 'link_feeds_set_target',
-            'description' => t('The title for the CCK !name field of the node.', array('!name' => $name)),
-            'real_target' => $field_name,
-          );
-        }
       }
     }
   }
 }
 
 /**
- * Callback for mapping to link field.
+ * Callback for mapping. Here is where the actual mapping happens.
  *
- * @param $node
- *   Reference to the node object we are working on.
- * @param $target
- *   The selected link CCK field.
- * @param $value
- *   The value to assign to the CCK field.
+ * When the callback is invoked, $target contains the name of the field the
+ * user has decided to map to and $value contains the value of the feed item
+ * element the user has picked as a source.
  */
-function link_feeds_set_target($node, $target, $value) {
-  module_load_include('inc', 'link');
-  if (!empty($value)) {
-    static $defaults = array();
-    list($field_name, $sub_field) = split(':', $target);
-
-    if (!isset($defaults[$node->type][$field_name])) {
-      $field = content_fields($field_name, $node->type);
-      $defaults[$node->type][$field_name]['attributes'] = $field['attributes'];
-      if (!in_array($field['title'], array('optional', 'required', 'none'))) {
-        $defaults[$node->type][$field_name]['title'] = $field['title_value'];
-      }
-    }
-    $field_data = isset($node->$field_name) ? $node->$field_name : array();
+function link_feeds_set_target($entity, $target, $value) {
+  if (empty($value)) {
+    return;
+  }
 
-    if (!is_array($value)) {
-      $value = array($value);
-    }
+  list($field_name, $sub_field) = split(':', $target);
 
-    $i = 0;
-    foreach ($value as $v) {
-      if ($v instanceof FeedsEnclosure) {
-        $v = $v->getValue();
-      }
-      if (!isset($field_data[$i])) {
-        $field_data[$i] = $defaults[$node->type][$field_name];
+  // Handle non-multiple value fields.
+  if (!is_array($value)) {
+    $value = array($value);
+  }
+
+  $info = field_info_field($target);
+
+  // Iterate over all values.
+  $i = 0;
+
+  foreach ($value as $v) {
+    if (!is_array($v) && !is_object($v)) {
+      if (strstr($target, 'url')) {
+        if(isset($entity->{$field_name}['und'][$i]['title'])) {
+          $field['und'][$i]['title'] = $entity->{$field_name}['und'][$i]['title'];
+        }
+        $field['und'][$i]['url'] = $v;
       }
-      if ($sub_field != 'url' || (($v = link_cleanup_url($v)) && valid_url($v, true))) {
-        $field_data[$i][$sub_field] = $v;
+      elseif (strstr($target, 'title')) {
+        if(isset($entity->{$field_name}['und'][$i]['url'])) {
+          $field['und'][$i]['url'] = $entity->{$field_name}['und'][$i]['url'];
+        }
+        $field['und'][$i]['title'] = $v;
       }
-      $i++;
     }
-
-    $node->$field_name = $field_data;
+    if ($info['cardinality'] == 1) {
+      break;
+    }
+    $i++;
   }
+
+  $entity->{$field_name} = $field;
 }
