Eliminate call to node_object_prepare(&$node) function
mustafau - November 29, 2007 - 10:56
| Project: | FeedAPI |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | postponed |
Jump to:
Description
node_object_prepare(&$node) function in node.module was moved to node.pages.inc in drupal-6.0-beta3. I think it should not be used outside the node module.

#1
node_object_prepare() calls the nodeapi('prepare') hooks, which are meant for:
(http://api.drupal.org/api/function/hook_nodeapi/5)
FeedAPI uses node_object_prepare() to simulate the creation of a node - modules get time to do their presets on the node (e. g. have a look at node_prepare()).
I don't really like the idea that we make other modules believe that we are about to show a node on the add/edit form, either. Whats a best practice for automatic node creation? What would be a good alternative?
Related: http://drupal.org/node/165388#comment-620821
#2
I guess calling
drupal_execute($form_id, $form_values)might do the same thing.#3
node_object_prepare() and node_prepare() functions are meant to simulate the demonstration of a node. I think in order to simulate the creation you should call node_submit().
The comment on node_submit() is:
<?php/**
* Prepare node for save and allow modules to make changes.
*/
?>
#4
Thanks for warning me, yes, i faced with huge problems when i started to implement "creating node via drupal_execute" task.
The most serious is the following:
I had a line similar to this: drupal_execute($old_node->type. '_node_form', $values, $node);
drupal_execute has NO return value if there is no problem and $node structure remains unaltered. There is only 2 way to get the nid : run a direct SQL query or do node_load() . Both of them are too expensive.
Anyway, I know that this IS a problem, because I faced with an issue, which roots are from here : http://drupal.org/node/195105 . So the core forum module uses form_alter to pass taxonomy-like data. And this data is lost now. That's why I started to implement node creation w/ drupal_execute, but it makes even bigger problems.
#5
Any progress on this issue?
#6
Well, because of the above problems, i do not plan to solve this for DRUPAL-5, but i have to solve it for FeedAPI for Drupal 6 (it will be available soon) and the solution can be backported (if it's possible)
#7
What about calling node_submit() instead of node_object_prepare().
#8
Please check out this thread:
http://lists.drupal.org/archives/development/2008-03/msg00063.html
Yes, your suggested change is reasonable, although there is no perfect way to do currently.
node_submit seems to be a really good candidate, but for example there is a permission checking inside it, i had to fill the uid manually, for example.
Soon I'll test the effects of changing node_object_prepare() lines to node_submit.
#9
We should try to do #7 - be aware that there are all sorts of ramifications in the module family - off the top of my head there are feedapi_mapper and feedapi_inherit that count on getting a call to 'prepare'. feedapi_node itself creates feed item nodes with calling 'prepare' but not 'submit'.
Alex
#10
#11
blogapi_blogger_new_post() is a good example of creating nodes programmatically. I think it is the only function that creates node programmatically in Drupal core.
http://api.drupal.org/api/function/blogapi_blogger_new_post
#12
Just out of curiosity - does anyone know if the above change was ever committed?