Hi,
I am not entirely clear on this, but as far as I can tell, Feeds overrides a few node hooks when importing nodes.
I am building a system which would greatly benefit from CSV Import. Using CCK and Node References, I have a system whereby Product nodes are listed, and multiple Price Update nodes are attached to a product to provide a daily price history.
eg.
- Product: Litre of Milk
- Price: 2010-11-01 - $3.15
- Price: 2010-11-02 - $3.10
I need a particular form of validation for this configuration. Each Price has to have a unique combination of 'date' and 'node reference' values, so that the system doesn't end up with duplicate prices for a product on a particular day. The individual values can have duplicates, but not the combination.
I've written a very simple validation module using hook_nodeapi to perform this validation on individual price submissions, but Feeds ignores this on a CSV import of price nodes.
Is there currently a way to tell Feeds to use a particular method of validation on nodes it imports (beyond the basic per-field uniqueness test already in place)?
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | node_import_with_form_validation_modules.zip | 16.37 KB | shaunjohnston |
Comments
Comment #1
alex_b commentedFeeds is not using validation as it is too tied into form submissions. Can you execute validation on a different level? Maybe by using a custom processor that extends the node processor?
Comment #2
David Goode commentedComment #3
shaunjohnston commentedI ended up using a fairly convoluted approach because I had invested a lot of time in using Form events (part of Rules) to perform my node validation.
My approach involved writing a module with a plugin for Feeds Import, modeled on the FeedsNodeProcessor plugin, with form based validation included. I had a few problems becuase D6 Form API is not built for batching (there are some long-standing legacy problems with static variables which cause a form instance to only validate once per Drupal call). To overcome them I ended up installing Drupal Override Function and runkit, and using this combination to override drupal_validate_form, _form_validate and form_set_error with custom versions (so as not to hack core).
The end result is a plugin for Feeds Import that performs form validation based on the content type being imported, for each node it attempts to import. If a node fails validation, Drupal will set an error message containing a dump of the invalid row and keep going. Once complete it will set a message stating the number imported.
I have attached the module I wrote here. Some of the code is heavy-handed - especially the part of the plugin that converts a node structure to a form structure for validation - at the moment it's working for me but it could probably be improved.
Hope this helps someone further down the line.
Comment #4
darrellduane commentedThanks for posting this, but I'm hopeful that someone has some ideas about a hook where I can put some custom code that will skip over a single row in an import done by the Node Import module. I put something in 'validate', and it clearly is being run, but throwing a form_set_error doesn't help. I'd put in a drupal_goto but I think that would stop the entire import. Any other ideas?
Comment #5
shaunlaws commentedI'm not sure exactly what the functionality of it is, because I couldn't find any documentation and I haven't dug into the code, but there is a 'unique' filter plugin in the Feeds Tamper module. If you can't use this for your purposes, it might nevertheless serve as a model for writing a Feeds Tamper filter that does what you need it to do.
Comment #6
giorgio79 commentedMind if I reopen this for D7? There are new maintainers now that Development Seed (Alex and David) no longer work on Drupal... I would love to see Feeds use basic Drupal hooks like hook validate and hook presave. I have some custom code, and hook validate is ignored for some reason.
I am using a standalone form.
PS: Node presave does work, so just copied my code over there for now...
Comment #7
colanI started doing some work on save hooks over at #1362378: Provide entity post-save hook. Should we keep these as two separate issues, or combine them?
Comment #8
giorgio79 commentedThanks Colan, that work seems to focus on custom hooks. I believe using the standard Drupal hooks like validation is essential, and Feeds can save a lot of effort. This way we can use all the other modules that offer lots of useful stuff, like Field Validation module that takes care of a lot of stuff. This should not be duplicated in Feeds through building custom unique mappers for example...
Comment #9
colanGotcha. If it's not running modules' update hooks either, then this would explain a new problem I'm having.
Comment #10
ohthehugemanatee commentedAny progress here? If we could call entity validation hooks as a normal part of the import, that would make a lot of sense and would solve a lot of problems. Is it possible to build a fake form submission for each imported object just to run validation and hooks?
I have a use case that I'm sure is common - users who don't validate the data in their CSV before import. Feeds creates users with spaces at the front of Name fields (not allowed), invalid email addresses... Basic validation on those fields would help a lot.
Comment #11
adamtong commentedi have the same problem for field validation during import too!
Comment #12
rahadgp commentedHi There,
You can add require validation using Feeds Tamper module. Also you can set some settings to each and every mapping fields
Comment #13
twistor commentedSorry. hook_validate() is not going to get called. It requires a form, and trying to fake a form submission is way too much work. The validation should be happening in hook_node_presave() or hook_entity_presave() anyway since a node can get saved via node_save() at any point in time.