Create multiple CCK nodes with single custom form

2019-05-10 11:31发布

问题:

I need a form which will allow creation of several related nodes at the same time. All of the nodes involve CCK fields.

I would like to use as much of CCK's built-in validation, submission, input widget, and security functionality as possible/practical.

What is the best way to accomplish this in Drupal 6? Are there 'best practices' or docs anywhere?

Here are 3 possibilities I can see. I would love feedback on whether any of these would work, or if there are even better options.

1.

  • start with the standard node creation form for content type foo.
  • modify the form by adding fields for content type bar, using hook form_alter [can cck widgets for content type bar be inserted directly?]
  • use a custom submit handler to create node of type bar when the form is submitted [can the standard cck handler be called? or do i need to 'manually' construct the node object, do my own validation, and use node_save?]

2.

  • create a new, custom form that concatenates the 'normal' node creation forms for the relevant content types.
  • then use hook form_alter to modify the forms as necessary.
  • allow standard cck submit handlers to do the work of creating the nodes.

3.

  • create a custom form from scratch
  • create the nodes in my own submit handlers, using node prepare, node save, etc.

If found documentation on re-using the standard node creation form, but creating multiple nodes at the same time is not mentioned.

Using hook nodeapi and hook form_alter is documented in a post on advomatic's site, but the particular method descrube seems to require polluting one of the content types with 'dummy' fields.

Thank you very much for your help!

回答1:

The advomatic guys posted a nice solution to this.

http://www.advomatic.com/blogs/jonathan-delaigle/multiple-nodes-single-node-submission



回答2:

why not just use hook_nodeapi to handle the node creation for certain content types.

just set up a test condition to see if $node->type = 'foo', and then run a function to create two nodes or however many, using the values from the predefined fields. you can even set hook_nodeapi to only run when the $op is almost ready to insert the node into the database, thus ensuring the object has been run through appropriate validation before being passed on to the new nodes that need to be created.

http://api.drupal.org/api/function/hook_nodeapi/6 this page has a list of all available operations for the $op variable and what they do.

I hope that helps



回答3:

If the 2nd type bar needs only one or two additional inputs (fields) from the user, I would go with your approach one.

But given your clarification it seems that foo and bar are sufficiently different and complex, so your approach two seems more reasonable.

Concatenate both forms into one and hide the bar fields that you want to populate from the foo fields (or node, after you created it). In the forms validate and submit functions, you'll have to separate the forms again so that you can call the standard validation/submit handlers for both separately.

I have not done this yet, so I'm not sure how well this will play with the cck functionality, but I would expect it to work reasonably well to give it a try.