Basic multiple values handling for content mapper

gionnibgud - February 2, 2009 - 14:02
Project:Feed Element Mapper
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

Hi I'm trying to figure out how to handle multiple values, al least for cck basic contents.

This is the code i came up with:
the idea is that the user maps the different feed items or csv columns to the same field mapper.
IF the cck field is set to multiple i add values to the array, if not i just write the first element.

<?php
// $Id: feedapi_mapper_content.inc,v 1.1.2.4.2.2 2008/10/27 00:07:34 alexb Exp $

/**
* On behalf implementation of hook_feedapi_mapper for content.module (CCK).
*
* @param string $op
* @param Drupal node $node
* @param string $field_name
* @param string, number or array of string or number $feed_element
* @param string or number as id $sub_field
*
*/
function content_feedapi_mapper($op, $node, $field_name, $feed_element = array(), $sub_field = '') {
 
// Test for the node field that we would like to map to.
 
if (strpos($field_name, 'field_') === 0) {
    if (
$op == 'describe') {
     
// Describe what we are doing in this mapper. This shows up as help text on the mapping page.
     
if (feedapi_mapper_content_is_cck_type($field_name, array('text', 'number_integer', 'number_decimal'))) {
        return
t('Maps a string or a number to this CCK field.');
      }
    }
    else if (
$op == 'list') {
     
// Here we are being asked to list sub fields we would like to map to.
     
if (feedapi_mapper_content_is_cck_type($field_name, array('text', 'number_integer', 'number_decimal'))) {
        return
TRUE;
      }
      return
FALSE;
    }
    else if (
$op == 'map') {
     
// Here is where the actual mapping happens.
      // When we are called at this point, $field_name contains the name of the field the user has
      // decided to map to and $field_element is the feed item element the user has decided to map.
      // We just need to put the two things together. The data structure here depends a lot on
      // CCK. We stick the value in $feed_element into $node->$field_name[0]['value'].
    


     
$field_settings = content_fields($field_name); // We need to check fields settings to know if field is multiple
     
    
     
if ($field_settings['multiple']==0) {
          
// single field
         
if (!is_array($feed_element)) {
           
$field = isset($node->$field_name) ? $node->$field_name : array();
           
$field[0]['value'] = $feed_element;
           
$node->$field_name = $field;
          }
      } elseif (
$field_settings['multiple']==1) {
           
// unlimited values field
         
if (!is_array($feed_element)) {
           
$field = isset($node->$field_name) ? $node->$field_name : array();
           
$field[]['value'] = $feed_element;
           
$node->$field_name = $field;
          }


      } elseif (
is_numeric($i = $field_settings['multiple'])) {
         
     
          if (!
is_array($feed_element)) {
             
$l = count($node->$field_name);
              if (
$l < $i) {
           
$field = isset($node->$field_name) ? $node->$field_name : array();
           
$field[]['value'] = $feed_element;
           
$node->$field_name = $field;
              }
          }
     
      }
      return
$node;
    }
  }
}

?>

First round of test was fine but i had only txt fields to test with.
cheers

#1

inocram - February 4, 2009 - 07:18

Did you actually replaced the content_feedapi_mapper() function from feedapi_mapper_content.inc?

#2

gionnibgud - February 4, 2009 - 16:36

Sure I did! But this code is just a proposal and it would be a good thing if someone else would look into it.
I guess I could make a patch but I'd rather wait for some feedback.
If you want to test it you can copy and paste (remove the final '?>'). But first backup your DB, do your vodoo, move around pencils on your desk or whatever you do when you don't want to screw up your drupal installation... ;) (most likely if there are errors it will just fail import and that's all!!)

#3

Summit - September 25, 2009 - 19:43

Subscribing, any progress in this field please?
greetings, Martijn

 
 

Drupal is a registered trademark of Dries Buytaert.