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.

Comments

alex_b’s picture

node_object_prepare() calls the nodeapi('prepare') hooks, which are meant for:

"prepare": The node is about to be shown on the add/edit form.

(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

mustafau’s picture

I guess calling drupal_execute($form_id, $form_values) might do the same thing.

mustafau’s picture

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:

 /**
 * Prepare node for save and allow modules to make changes.
 */ 
Aron Novak’s picture

Status: Active » Postponed (maintainer needs more info)

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.

mustafau’s picture

Any progress on this issue?

Aron Novak’s picture

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)

mustafau’s picture

What about calling node_submit() instead of node_object_prepare().

Aron Novak’s picture

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.

alex_b’s picture

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

mustafau’s picture

Version: 5.x-1.x-dev » 6.x-1.x-dev
Status: Postponed (maintainer needs more info) » Postponed
mustafau’s picture

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

newmediaist’s picture

Just out of curiosity - does anyone know if the above change was ever committed?

kentr’s picture

Component: Code » Code feedapi (core module)

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.

Not in Drupal core, of course, but perhaps an example: Mailhandler creates new nodes via email.