Port FeedAPI Mappers: link

alex_b - November 4, 2009 - 18:09
Project:Feeds
Version:6.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:needs work
Issue tags:Novice
Description

Port the taxonomy mapper from Feed Element Mapper.
Port tests for it.
Document.

#1

alex_b - November 4, 2009 - 18:10

Port the *link mapper of course.

#2

alex_b - November 4, 2009 - 18:32

As an example for the API, take a look at mappers/content.inc should be used. For documenting the Mapping API, I opened an issue #623466: Document mapping API.

#3

mongolito404 - November 19, 2009 - 12:24

I need this for my project so I started working on it.

I need to write some tests and create a patch file but here what i have at the moment

<?php
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 (
$field['type'] == array('link')) {
       
$name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
       
$targets[$field_name.'#url'] = array(
         
'name' => $name . ' (' . t('url').')',
         
'callback' => 'link_feeds_set_target',
         
'description' => t('The URL for the CCK !name field of the node.', array('!name' => $name)),
        );
        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)),
          );
        }
      }
    }
  }
}

function
link_feeds_set_target($node, $target, $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'];
    }
  }
   
 
$field_data = isset($node->$field_name) ? $node->$field_name : array();

  if(!
is_array($value)) {
     
$value = array($value);
  }

 
$i = 0;
  foreach (
$value as $v) {
    if(!isset(
$field_data[$i])) {
     
$field_data[$i] = $defaults[$node->type][$field_name];
    }
    if (
$sub_field != 'url' || (($v = link_cleanup_url($v)) && valid_url($v, true))) {
     
$field_data[$i][$sub_field] = $v;
    }
   
$i++;
  }
 
$node->$field_name = $field_data;
}
?>

I don't know if using hash in the targets for subfields is the right way of doing it but it works.

#4

mongolito404 - November 24, 2009 - 09:21
Status:active» needs review

Here is a patch file against HEAD with the mapper in #3 and its test case.

AttachmentSize
623444-4_mapper_link.patch 7.26 KB

#5

alex_b - November 25, 2009 - 01:52
Status:needs review» needs work

Contains tabs as indents...

#6

mongolito404 - November 25, 2009 - 07:44
Status:needs work» needs review

Sorry for the tabs, here is a fixed patch.

AttachmentSize
623444_6_mapper_link.patch 7.52 KB

#7

renee - November 26, 2009 - 02:11

The patch in #6 worked beautifully for me.

#8

mylos - November 26, 2009 - 12:03

Yes also for me #6 is working ok. I import some CSV data with affiliate link and used this patch to map it to CCK link field.
You can see example here:
http://www.pine-chest-of-drawers.com/

#9

fabsor - December 5, 2009 - 14:27

I just want to confirm that this patch worked flawlessly for me to!

#10

BWPanda - December 7, 2009 - 01:01
Status:needs review» reviewed & tested by the community

Working for me too.

#11

alex_b - December 7, 2009 - 04:09
Status:reviewed & tested by the community» needs work

- Did some minor cleanup
- Tests throw 2 exceptions in the same place (getFormFieldsValues())
- getFormFieldsNames() and getFormFieldsValues() are missing comments
- I am not 100 % sure what the extended logic in link_feeds_set_target() is doing - mongolito404, could you comment that?
- Changed delimiters from # to : as this is the route we started taking with taxonomy mapper.

AttachmentSize
623444-11_mapper_link.patch 7.89 KB

#12

rjbrown99 - December 8, 2009 - 05:29

I tested the patch from #11 with a CSV import feed with 3 different URL sources/targets. All of them worked for me. Just wanted to provide some feedback.

#13

Summit - December 19, 2009 - 16:47

Subscribing, greetings, Martijn

#14

mongolito404 - December 20, 2009 - 11:12

In link_feeds_set_target()
- $defaults is used to store the default title and attributes for the field
- Entries in $defaults are indexed by node type and field name ($defaults[$node->type][$field_name])
- For each value, the defaults are assigned to the field if not already set
- Each URL value is cleaned and validated by (($v = link_cleanup_url($v)) && valid_url($v, true))
- Title values are not validated and directly assigned to the field

 
 

Drupal is a registered trademark of Dries Buytaert.