I'm creating a custom node module, and am having trouble deciding which hooks to use for various processing tasks. What I'm intending to do is patch together various form values to create the "title" and "body" for my node. Initially I did this in hook_insert and hook_update, which worked well except for one thing. Since these hooks are only called on "submit", when I "preview" the node, it has no title nor body. So I need to place this code into a function that is called on both preview AND submit. Any ideas?

Comments

pwolanin’s picture

Usually I would try hook_submit to change the node before it's saved:

http://api.drupal.org/api/4.7/function/hook_submit

But, from looking at the functions that generate the preview:

http://api.drupal.org/api/4.7/function/node_form_add_preview

http://api.drupal.org/api/4.7/function/theme_node_preview

The only way you'll be able to see such "computed" fields in the preview are:

1) do this at the hook_validate stage using form_set_value()

2) save all the form values to the database as separate items in a new table, and then generate the output using hook_view.

I think #2 is the better (normal) way to go for the node content (body). Maybe you'll have to use #1 for the title.

However, I think this behavior is a bug- probably node_submit() should be called by node_preview so that the "submitted" node is in the preview. You can follow up to this bug report: http://drupal.org/node/104047

---
Work: BioRAFT

dunghopper’s picture

> 1) do this at the hook_validate stage using form_set_value()

Your first suggestion seems do-able. I'll give that a try.

> 2) save all the form values to the database as separate items in a new table, and then generate the output using hook_view.

I agree that hook_view seems to be the natural and intended (or "normal") way to create a 'computed' $body prior to rendering the node. But this also seems to bring me back to the initial problem. Generating the output with hook_view works for viewing the node AFTER it has been submitted or updated, but for a preview it doesn't work, because the values used in hook_view are loaded from the database (and of course, for a preview, they haven't yet been stored in the database). Am I missing something, or does this suggestion only apply to viewing the node after SUBMIT?

Maybe my implementation of hook_view is just wrong. I'll play with it, and see if I can make it work both for previews AND for rendering actual submitted nodes, without too much hacking.

Thanks for your advice.

P.S. I don't think it is a bug. Isn't the whole point of a 'preview' to see what your node will look like WITHOUT submitting it to the database? If the preview button was intended to submit the node, then there is no reason to have separate preview and submit buttons.

pwolanin’s picture

node_submit does not actually save anything to the database. That's node_save ;-)

Also, I think #2 will work, since let's say you define a form item 'first_name'. The node object that gets passed in for the preview will include the corresponding form value as $node->first_name. Thus, you can generate the computed output via hook_view even if the node has not yet been saved to the DB.

---
Work: BioRAFT

Zoologico’s picture

I created a custom content type using CCK.
I understand that I should use NODEAPI to hook into the creation and rendering process.
I am finding the same problem and that is that I cannot affect the preview of the node with any operations from nodeapi.

Any suggestions?