0

I've created a feeds importer (CSV parser) and I need to import data that is not in the source csv, into a field of my content type.

The field in the content type (taxonomy term) is:

enter image description here

My feeds importer machine name is: football_data_co_uk and the Source and Target field are set as this:

enter image description here

In my module, I'm using the following code to import the data:

function soccer_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
  // Set a custom value setter callback for the my field on my node type.
  if ($entity_type == 'node' && $bundle == 'football_data_co_uk_games') {
    if (isset($targets['field_season'])) {
      $targets['field_season']['callback'] = 'soccer_set_season_value';
    }
  }
}

function soccer_set_season_value(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  if ($source->id == 'football_data_co_uk') {
        if (!empty($mapping['season'])) {
            $entity->{$target}[$entity->language][0]['tid'] = $mapping['season'];
        }
    }
}

function soccer_import() {
  $feeds_importer = 'football_data_co_uk';
  $feeds_source = feeds_source($feeds_importer);

  $season = '1819';
  $path = 'http://football-data.co.uk/mmz4281/1819/P1.csv';

  // Get the current field mappings. 
  $field_mappings = $feeds_source->importer->processor->getMappings(); 

  foreach ($field_mappings as &$mapping) {
    // Add an arbitrary value to the mapping of field_multijob_build for 
    // use later in the processor target callback. 
    if ($mapping['target'] == 'field_season') {
      $mapping['season'] = $season;
    }
  }

  // Set the new field mappings. 
  $feeds_source->importer->processor->addConfig(array('mappings' => $field_mappings)); 

  // The previous call to FeedsProcessor::getMappings() cached 
  // the mapping, so we need to clear that before we run the import. 
  drupal_static_reset('FeedsProcessor::getMappings');

  $feeds_config = $feeds_source->getConfigFor($feeds_source->importer->fetcher);
  $feeds_config['source'] = $path;

  $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());
}

When I call the soccer_import function, my data is imported, the nodes are created, but the season field is not.

I have managed to implement this before but the source file was xml and not csv. In the mapping configuration I added a blank source. In this case, that option is not available.

Can someone help me figure out what I'm doing wrong?

Thank you.

Your Answer

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

Browse other questions tagged or ask your own question.