? 944986-5_link_mapper.patch
? 944986-6_link_mapper.patch
Index: feeds.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/feeds/feeds.info,v
retrieving revision 1.7.2.4
diff -u -p -r1.7.2.4 feeds.info
--- feeds.info	27 Oct 2010 19:53:21 -0000	1.7.2.4
+++ feeds.info	29 Oct 2010 20:58:10 -0000
@@ -20,6 +20,7 @@ files[] = tests/feeds_processor_node.tes
 files[] = tests/feeds_processor_term.test
 files[] = tests/feeds_processor_user.test
 files[] = tests/feeds_scheduler.test
+files[] = tests/feeds_mapper_link.test
 files[] = tests/parser_csv.test
 files[] = views/feeds.views_default.inc
 files[] = views/feeds_views_handler_argument_importer_id.inc
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	29 Oct 2010 20:58:10 -0000
@@ -1,87 +1,78 @@
 <?php
-// $Id: link.inc,v 1.5 2010/09/17 17:25:19 alexb Exp $
+// $Id$
 
 /**
  * @file
- * On behalf implementation of Feeds mapping API for link.module (CCK).
+ * On behalf implementation of Feeds mapping API for link.module.
  */
 
 /**
- * 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);
-
-  $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)),
+function link_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'] == 'link_field') {
+      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($source, $entity, $target, $value) {
+  if (empty($value)) {
+    return;
+  }
 
-    if (!is_array($value)) {
-      $value = array($value);
-    }
+  // Handle non-multiple value fields.
+  if (!is_array($value)) {
+    $value = array($value);
+  }
 
-    $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];
+  // Iterate over all values.
+  $i = 0;
+  $info = field_info_field($target);
+  list($field_name, $sub_field) = explode(':', $target);
+  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;
 }
Index: tests/feeds_mapper_link.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/feeds/tests/feeds_mapper_link.test,v
retrieving revision 1.5.4.2
diff -u -p -r1.5.4.2 feeds_mapper_link.test
--- tests/feeds_mapper_link.test	6 Oct 2010 14:12:43 -0000	1.5.4.2
+++ tests/feeds_mapper_link.test	29 Oct 2010 20:58:10 -0000
@@ -26,16 +26,14 @@ class FeedsMapperLinkTestCase extends Fe
    */
   public function setUp() {
     // Call parent setup with the required module
-    parent::setUp(
-      'feeds', 'feeds_ui', 'ctools', 'job_scheduler', 'content', 'link'
-    );
+    parent::setUp(array('link'));
 
     // Create user and login
     $this->drupalLogin($this->drupalCreateUser(
         array(
           'administer content types',
           'administer feeds',
-          'administer nodes',
+          'bypass node access',
           'administer site configuration',
         )
     ));
@@ -50,32 +48,28 @@ class FeedsMapperLinkTestCase extends Fe
     // Create content type.
     $typename = $this->createContentType(NULL, array(
       'alpha' => array(
-        'type' => 'link',
+        'type' => 'link_field',
         'settings' => array(
-          'title' => 'required',
-          'multiple' =>  '0',
+          'field[settings][title]' => 'required',
         ),
       ),
       'beta' => array(
-        'type' => 'link',
+        'type' => 'link_field',
         'settings' => array(
-          'title' => 'none',
-          'multiple' => '0',
+          'field[settings][title]' => 'none',
         ),
       ),
       'gamma' => array(
-        'type' => 'link',
+        'type' => 'link_field',
         'settings' => array(
-          'title' => 'optional',
-          'multiple' =>  '0',
+          'field[settings][title]' => 'optional',
         ),
       ),
       'omega' => array(
-      'type' => 'link',
+      'type' => 'link_field',
         'settings' => array(
-          'title' => 'value',
-          'title_value' => $static_title,
-          'multiple' =>  '0',
+          'field[settings][title]' => 'value',
+          'field[settings][title_value]' => $static_title,
         ),
       ),
     ));
@@ -148,9 +142,9 @@ class FeedsMapperLinkTestCase extends Fe
    */
   protected function getFormFieldsNames($field_name, $index) {
     if(in_array($field_name, array('alpha', 'beta', 'gamma', 'omega'))) {
-      $fields = array("field_{$field_name}[{$index}][url]");
+      $fields = array("field_{$field_name}[und][{$index}][url]");
       if (in_array($field_name, array('alpha', 'gamma'))) {
-        $fields[] = "field_{$field_name}[{$index}][title]";
+        $fields[] = "field_{$field_name}[und][{$index}][title]";
       }
       return $fields;
     }
@@ -163,7 +157,6 @@ class FeedsMapperLinkTestCase extends Fe
    * Override parent::getFormFieldsValues().
    */
   protected function getFormFieldsValues($field_name, $value) {
-    $field = content_fields($field_name);
     if (in_array($field_name, array('alpha', 'beta', 'gamma', 'omega'))) {
       if (!is_array($value)) {
         $value = array('url' => $value);
Index: tests/feeds_mapper_test.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/feeds/tests/feeds_mapper_test.inc,v
retrieving revision 1.2.4.1
diff -u -p -r1.2.4.1 feeds_mapper_test.inc
--- tests/feeds_mapper_test.inc	29 Sep 2010 23:56:10 -0000	1.2.4.1
+++ tests/feeds_mapper_test.inc	29 Oct 2010 20:58:10 -0000
@@ -25,7 +25,7 @@ class FeedsMapperTestCase extends FeedsW
     'emaudio' => 'emaudio_textfields',
     'filefield' => 'filefield_widget',
     'image' => 'imagefield_widget',
-    'link' => 'link',
+    'link_field' => 'link_field',
     'number_float' => 'number',
     'number_integer' => 'number',
     'nodereference' => 'nodereference_select',
