Skip to content

Instantly share code, notes, and snippets.

@sanguis
Created April 9, 2012 17:13
Show Gist options
  • Save sanguis/2344800 to your computer and use it in GitHub Desktop.
Save sanguis/2344800 to your computer and use it in GitHub Desktop.
feeds field mapping examples
<?php
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*/
function foo_contact_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'], array('foo_fields_contact'))) {
$targets[$name] = array(
'name' => $instance['label'],
'callback' => 'foo_contact_feeds_set_target',
'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
);
}
}
}
/**
* 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 foo_contact_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
// Make sure $value is an array of objects of type FeedsEnclosure.
if (!is_array($value)) {
$value = array($value);
}
$i = 0;
$info = field_info_field($target);
// dpm($info);
$fields = array('title', 'first_name', 'middle_name', 'last_name', 'position', 'phone');
//Populate Entity
//dpm($value, "values");
foreach ($value as $v) {
// dpm($v, $i);
if (!is_array($v) && !is_object($v)) {
$vs = explode(';', $v);
foreach($fields as $k => $t) {
if (!empty($entity->{$target}['und'][$i][$t])) {
$field['und'][$i][$t] = $entity->{$target}['und'][$i][$t];
}
$field['und'][$i][$t] = $vs[$k];
}
}
if ($info['cardinality'] == 1) {
break;
}
$i++;
}
// dpm($field, '');
// dpm($i, "I");
$entity->{$target} = $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment