I need support to set a field on a feeds mapping that is not in the source.
I already have some code that for some reason is not working.
If I use hook_feeds_after_parse, I can set the value but I need to use the method below (new mapping).
function MYMODULE_cronapi($op, $job = NULL) {
$items['TEST IMPORT'] = array(
'description' => 'TEST IMPORT',
'rule' => '1 * 1 * *',
'callback' => ' MYMODULE_testImport',
);
return $items;
}
function MYMODULE_testImport() {
$test_field_value = 'this is a test';
$feeds_importer = 'test_feeds';
$feeds_source = feeds_source($feeds_importer);
$field_mappings = $feeds_source->importer->processor->getMappings();
$path_to_xml = 'http://.../sites/default/files/test_content.xml';
foreach ($field_mappings as &$mapping) {
if ($mapping['target'] == 'field_test_field') {
$mapping['test_field'] = $test_field_value;
}
}
$feeds_source->importer->processor->addConfig(array('mappings' => $field_mappings));
drupal_static_reset('FeedsProcessor::getMappings');
$feeds_config = $feeds_source->getConfigFor($feeds_source->importer->fetcher);
$feeds_config['source'] = $path_to_xml;
$config = array(
'process_in_background' => TRUE,
);
$feeds_source->setConfigFor($feeds_source->importer->fetcher, $feeds_config);
$feeds_source->importer->addConfig($config);
$feeds_source->save();
$feeds_source->startImport();
while (FEEDS_BATCH_COMPLETE != $feeds_source->import());
}
function MYMODULE_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
if ($entity_type == 'node' && $bundle == 'test_content') {
if (isset($targets['field_test_field'])) {
$targets['field_test_field']['callback'] = 'MYMODULE_set_test_field';
}
}
}
function MYMODULE_set_test_field(FeedsSource $source, $entity, $target, array $values, array $mapping) {
if ($source->importer->id == 'test_feeds') {
if (!empty($mapping['test_field'])) {
$entity->{$target}[$entity->language][0]['value'] = $mapping['test_field'];
}
}
}