1

I am trying to modify data coming from a feed before it saved to a node by using hook_feeds_presave().

I have code that looks like this:

my_module_feeds_presave(FeedsSource $source, $entity, $item) {
     $email =  $item['email address'];
    //Remove "NOT GIVEN" from the email column to stop errors
    if($email == "NOT GIVEN"){
        $entity->field_email['und'][0]['value'] = '';
    } 
}

I would think that when the node is saved that value in the email filed would be blank. However, it is still saving the value of NOT GIVEN.

If anyone can please point me in the right direction with this I would appreciate it.

Thanks,

Josh

2
  • 1
    did you try the patch in this issue ?
    – berramou
    Jan 24, 2019 at 14:00
  • Thank you for the issue but this patch is for sure in my version as I am getting the correct value from the $issue variable.
    – Josh
    Jan 25, 2019 at 19:14

2 Answers 2

1

Can you mention your Drupal version?

  • For Drupal 7, your code looks good enough at a first glance.
  • If it's Drupal 8, all you need is $entity->field_email = ''.

If that doesn't work, leave a comment and I'll take a detailed look.

1
  • It is Drupal 7 and it does not work. The node API is still seeing NOT GIVEN.
    – Josh
    Jan 24, 2019 at 1:56
1

If you are using the email field from the Email Field module, then the column you need to save the value under is 'email', not 'value'.

/**
 * Implements hook_feeds_presave().
 */
function my_module_feeds_presave(FeedsSource $source, $entity, $item) {
  if ($source->importer->id != 'my_importer') {
    // No modification needed for this importer.
    return;
  }

  $email = $item['email address'];
  // Remove "NOT GIVEN" from the email column to stop errors.
  if ($email == "NOT GIVEN"){
    $entity->field_email['und'][0]['email'] = '';
  } 
}

Note that you can alternatively also use the hook hook_feeds_after_parse() to modify the parsed data:

/**
 * Implements hook_feeds_after_parse().
 */
function my_module_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  if ($source->importer->id != 'my_importer') {
    // No modification needed for this importer.
    return;
  }

  // Remove mail addresses that contain 'NOT GIVEN'.
  foreach ($result->items as &$item) {
    if (isset($item['email address']) && $item['email address'] == 'NOT GIVEN') {
      $item['email address'] = '';
    }
  }
}

Note that in the code examples above I added code to restrict modification to a particular importer. This is recommended if you are using multiple importers and the modification should only be applied to a single importer.

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.