2

I'm using Feeds import module, after importing CSV file, if a row doesn't match content type validation the feeds import saves that node with empty space in that field.

Question: Now i want FeedsNodeProcessor to validate the node, and if there is a empty space it should throw error for that node or should not create node for that row.

Example: If i have 40 rows in an excel and 39 rows satisfies the conditions of content type and only 1 row doesn't then it has to like this, 39 nodes created.

Updated: i have added hook_feeds_prevalidate() in my hook_form_alter module.

 function tracker_custom_form_alter(&$form, &$form_state, $form_id) {
   if($form_id == 'orientation_node_form'){
      $form['#validate'][] = 'validate_orientation';
}
}
function validate_orientation($form,&$form_state)
{
....
}
function tracker_custom_feeds_prevalidate($source, &$entity, &$item, $entity_id) {
//      dpm($entity->feeds_item->id); - its not printing anything.
//      dpm($item); - its not printing anything.
  if ($entity->feeds_item->id == 'bulk_upload') {
     if (empty($item['Billing Type'])) {
         $entity->feeds_item->skip = TRUE;
         drupal_set_message('ksdhbf');
   }
 }
}
  • 1
    Drupal version? – No Sssweat Oct 30 '19 at 11:56
  • I am using Drupal 7 – Harish Nov 5 '19 at 11:11
1

You can add this functionality in a custom module. But you'll have to add the logic that you have in your node's validation here.

/*
 * Implements hook_feeds_prevalidate()
 */
function mymodule_feeds_prevalidate($source, &$entity, &$item, $entity_id) {
  // the $item contains your csv files column values
  if ($entity->feeds_item->id == 'import_name') { // check the feed import ID
    if ($item['some column'] != 'something') { // validate column value
      $entity->feeds_item->skip = TRUE; // skips this row from importing
    }
  }
}

Important: I think you'll need 7.x-2.x-dev in order for this hook to work, I'm using it. This is a new hook and I think it has not been added to 7.x-2.0-beta4 yet.

  • Thanks for your response. I copied this code and paste it in my custom module(Already using for other validations) function mymodule_feeds_prevalidate($source, &$entity, &$item, $entity_id) { // the $item contains your csv files column values if ($entity->feeds_item->id == 'bulk_upload') { // check the feed import ID if ($item['Billing Type'] == ' ') { // validate column value $entity->feeds_item->skip = TRUE; // skips this row from importing } } } Not working. Whether i have to create a new C-module for this or i can paste it in my previous module. – Harish Nov 6 '19 at 10:02
  • @Harish if you already have an existing custom module, you can put the code there. Clearly some if statement is failing to pass. I suggest you install Devel and start inspecting variables dpm($entity->feeds_item->id); to check you have the correct ID and dpm($item); to make sure your "Billing Type" is indeed correct. Maybe also try changing your if statement to if (empty($item['Billing Type'])) { – No Sssweat Nov 6 '19 at 10:16
  • Im new to Drupal, im using hookformalter in my module, so whether i have to add this function directly after that formalter and its [validate][] functions? And i tried that dpm() but in status its empty, nothing is displayed. – Harish Nov 6 '19 at 12:42
  • 1
    Yahh, now its working very good(I tried it in new Module). Thanks a lot. – Harish Nov 14 '19 at 9:13
  • 2
    @Harish that will need to be a separate question. – No Sssweat Nov 14 '19 at 9:42

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.