[development] hook_nodeapi vs. $form['#submit']
drupal at dave-cohen.com
Mon Jul 10 01:57:23 UTC 2006
I'm building a site which has two custom node types with a one-to-many
relationship. That is, one type is a container for items of the second type.
When creating/editing container nodes, I want the form to allow the user to
choose from nodes of the contained type. Similarly when creating/editing the
contained type the user may choose a container in which to place it. I know
about the node relativity module, but without going off on a tangent I don't
think its appropriate for this case.
The way I see it, I have two ways to implement this. Because I'm defining the
two node types, I can put all the code I need into hook_form, hook_insert and
hook_update. That's the first option.
Another option is to not modify the modules defining the node types, and
instead introduce another module which is aware of the two types and defines
the relationship between them. In this module I would use hook_form_alter to
add what is needed in the edit forms and hook_nodeapi for the insert and
update events. I'm partial to using this second option, because in the end
I'll have a nifty code snippet that I could use anytime I want to make one
node type a collection of other node types.
So my question is, for the second option, do I really need hook_nodeapi?
Wouldn't it be nice to use hook_form_alter to add fields to the forms, and
define $form['#submit'] to call callbacks to handle inserts and updates?
That way, my callbacks would only be called when necessary whereas
hook_nodeapi gets called an aweful lot (every node load, for instance).
And my answer is yes, I really do need to define hook_nodeapi. Because the
$form['#submit'] callback does almost what I need, but not quite everything.
In particular, when the submit callback is called during a node insert, there
is no way to learn the new node's id. And for my code the node id is
What do you think? Would it make more sense to use the combination of
hook_form_alter and $form['#submit'] callback, rather than hook_form_alter
and hook_nodeapi? Is there some way to get the inserted node id that I'm not
aware of? Should there be a way?
More information about the development