0

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'];
        }
    }
}
0
  1. To get the importer's machine name from the source, instead of doing $source->id, this should be $source->importer->id.

  2. I don't know what you are trying to do with $mapping['_ad_area'] exactly. $mapping contains target configuration (aka mapping settings). I don't see form_callbacks on your target code. The default taxonomy reference field also doesn't have an option called '_ad_area'. If you added mapping for the 'field_ad_area' target, values coming from the source should exist in $values.

  3. You say you want to provide a value that is not in your original source. If this is a fixed value, you can use the Tamper plugin "Set default value" from the Feeds Tamper module. If you are using the CSV parser, on the mapping form you can just enter a source column name that does not exist in your CSV file. Otherwise, select "Blank source".

    If you programmatically want to set a value, you can implement the hook hook_feeds_after_parse() in a custom module:

/**
 * Implements hook_feeds_after_parse().
 */
function mymodule_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  if ($source->importer->id != 'qc_data_generic_import') {
    return;
  }

  foreach ($result->items as &$item) {
    $item['Blank source 1'] = 'My value';
  }
}
  • thanks for your help. I think the hook_feeds_after_parse() doesn't help in my case because I'm getting the values I want to add to the blank sources in another place. To be easier and more complete, I've created a new test content type and feeds especifically for the case I need. The complete code is here: dpaste.com/3A78Z2M. On line 13, I define the variable and the value that I want to insert in my node. The content type machine name is "test_content" and has a title field and the "field_test_field". – zephirus Apr 2 at 10:22
  • Forgot to say that the value(s) I want to add are not fixed, in my above example, it's fixed but in my (more complex code) I'm getting those values from another source. Basically, I define them in the function "MYMODULE_testImport" – zephirus Apr 2 at 10:33
  • I also tried your solution using hook_feeds_after_parse() and it doesn't set the values, also – zephirus Apr 2 at 14:26
  • You can also implement a custom source with the hook hook_feeds_parser_sources_alter(). This hook is invented to be able to add in values from other sources. See feeds.api.php. Not sure if this is helpful in your case as you said implementing hook_feeds_after_parse() didn't work for you. – MegaChriz Apr 3 at 11:58
  • I already tried with hook_feeds_processor_targets_alter (as you can see in the code attached) and still doesn't work. Maybe I have something wrong in the MYMODULE_testImport function that makes all the hooks I tried, to not work? – zephirus Apr 3 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.