hook_nodeapi vs. $form['#submit']
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 necessary. 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? Thanks, -Dave
I did something similar for a project a few months ago before 4.7 was released. The nodes had one to many relations, in multiple levels, let us call them parent (p), child (c) and grandchild (gc). Each parent will eventually have one or more children, and each child will have one or more grandchildren. In p, if the permissions are OK, a p_links() will show "add c", and similarly, c would show "add gc". Each of those would be a link to node/add/c or node/add/gc. In c, there would be a drop down list (#select) of all nodes of type p (there are not thousands of them), and similarly, in gc, there would be a drop down for type c. The creator of the node selects who the parent is. Then nodeapi(view) for p, will call c_list(), which is in c.module. Same idea for c and gc. The modules' code is sort of intermingled, but that is OK for this project.
participants (2)
-
Dave Cohen -
Khalid B