How do I add validation in the node processor before creating a node?
-
19-02-2021 - |
Question
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');
}
}
}
Solution
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.