My node has a field-collection field type (shirt). The field-collection "shirt" has three subfields - color, price, size.

I'm trying to create a mapper for these fields. Below is what I have so far (it's hard coded just to get it done and test). However, nothing appears in my mapping targets or in querypath to identify the source -- basically it's not working :0). I'm using QueryPath as the parser and NodeProcessor.

Please take a look and let me know where I'm going wrong. (Step two will be if it actually works with field-collection...but that's another issue).

shirt.inc file in feeds/mapper

/**
 *  For Field Collection mapper - Implements hook_feeds_processor_target_alter().
 *
 * @param $targets array of target fields
 * @param $entity_type
 * @param $bundle_name
 */
function shirt_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 (in_array($info['type'], 'field_collection')) {
		$callback = 'shirt_feeds_set_target';
      		$module = 'field_collection';
   		$field_name = 'field_shirt';
	
			$targets[ $field_name .':field_color'] = array(
			  'name' => $module . " module: " . $field_name . '.' . t('color'),
			  'callback' => $callback,
			  'description' => t('The color for the field-collection of the node.'),
			);
			$targets[ $field_name .':field_price'] = array(
			  'name' => $module . " module: " . $field_name . '.' . t('price'),
			  'callback' => $callback,
			  'description' => t('The price for the field-collection of the node.'),
			);
			$targets[ $field_name .':field_size'] = array(
			  'name' => $module . " module: " . $field_name . '.' . t('size'),
			  'callback' => $callback,
			  'description' => t('The size for the field-collection of the node.'),
			);
    		}
  	}
}
  
/**
 * Callback for mapping. Here is where the actual mapping happens.
 *
 * 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 shirt_feeds_set_target($source, $entity, $target, $value) {
  list($field_name, $sub_field) = explode(':', $target);
  if (strpos($sub_field, '][') !== FALSE) {
    list($sub_field, $last_field) = explode('][', $sub_field, 2);
  }

  if (!is_array($value)) {
    $value = array($value);
  }
  foreach ($value as $i => $val) {
    if (isset($last_field)) {
        $entity->{$field_name}[$i][$sub_field][$last_field] = $val;
      }
    else {
        $entity->{$field_name}[$i][$sub_field] = $val;
      }
    }

  return $entity;
}

Comments

mrfelton’s picture

I suspect this will be solved when #1033202: [Meta] Generic entity processor lands.

gaëlg’s picture

dasjo’s picture

#1 would be my favorite + i guess this is the most active discussion: #1063434: Add Feeds integration to FieldCollection

finex’s picture

Did someone tried this http://drupal.org/project/field_collection_feeds ? Sorry, I didn't follow the link before :-)

lotyrin’s picture

Status: Active » Closed (duplicate)

This appears to be a duplicate of #1063434: Add Feeds integration to FieldCollection. Lets take any discussion there.