1

I need to import a csv that has only two fields (date and name) to a content type that has 3 fields:

  • date
  • name
  • file

The file field should be set as the name of the file being imported.

My code until now is this:

function MYMODULE_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  $targets['my_feed'] = array(
    'name' => t('field_filename'),
    'description' => t('This field has the csv file name.'),
    'callback' => 'mymodule_import_filename',
  );
}

function MYMODULE_import_filename($source, $entity, $target, $value, $mapping) {
    $my_filename ='file.csv';
    $entity->{'field_'.$target}[$entity->language][]['target_id'] = $my_filename;
}

Where am I doing a mistake? Is this the way to import a value outside the file being imported?

Thanks.

2

I managed to get the answer to this question.

In MYMODULE_import_filename() we can get the data we want to import, in my case is just a simple text: 'file.csv'.

The simple way to do it is like this:

function MYMODULE_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  $targets['my_feed'] = array(
    'name' => t('field_filename'),
    'description' => t('This field has the csv file name.'),
    'callback' => 'mymodule_import_filename',
    'real_target' => 'field_filename',
  );
}

function MYMODULE_import_filename($source, $entity, $target, $value, $mapping) {
    $my_filename ='file.csv';
    $wrapper = entity_metadata_wrapper('node', $entity);
    $wrapper->field_filename->set($my_filename);
}

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.