Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal Custom Node Form

I want to create an input form for registered user, separated from admin's content creation form. Each submission will create multiple node. To illustrate the case, I will use content type Project, and Review.

Project: Title, Description, Owner, Rating.

Review: Title, Reviewer, Project, Difficulty

I have setup the content types along with their fields using CCK modules. I want to create an input form to be displayed for registered members (non-admin). Its field includes Project Name, Description, Owner, Rating, Review, Difficulty.

When the form is submitted, Project Name, Description, Owner, and Rating value goes into new Project node, where the rest goes into new Review node.

Another customization that I would like to do is for Rating and Difficulty input field to use star rating input.

What would be the best way to accomplish this? Should I create custom module and custom form (Can anyone point me how to do this)? Or is there any modules I could use?

Thanks

like image 867
Ferry Avatar asked Nov 05 '09 20:11

Ferry


3 Answers

There are several ways to do this:

  1. Do everything from scratch: This is what theunravelers suggestion (+1) boils down to - build the form yourself, add your own validation and submit handlers and on submit, build two node objects and save them. You'll have full control/flexibility, but it is quite some work and you need to have a good understanding of Drupals inner workings to get it right.

  2. 'Overload' one of your content types with the fields needed by the other and tweak the 'overloaded' content types submit (and partially edit/display) logic to create the other content type from the additional fields, while hiding them on the 'overloaded' one on display and edit. You can find an article describing this approach here. This is a considerably easier approach than #1, but I'd consider it to be a bit 'hackish', because of the content type definition vs. display mismatch.

  3. A less 'hackish' variation of #2 would be to set up your content types normally and just manipulate the edit and submit process via hook_form_alter(). You'd do more or less the same as with approach #2, but instead of 'overloading' one node with the additional fields upfront, you'd just inject them into the edit form directly on hook_form_alter (either from scratch or by loading the edit form of the other node in the background and copying the relevant field definitions from that). On form submission, you remove those additional fields while using them to build your other node. This requires a bit more work and knowledge than #2, but should be a bit more stable and easier to adjust/maintain than that, as you do not have to deal with a content type definition vs. display mismatch.

Also, you did not specify how you want to deal with editing of existing nodes - I'd suggest adding a nodereference to one of the nodes to keep track of their association. That way, you could also add the logic to edit both nodes from one form, as well as synchronized deletion, if desired.

like image 56
Henrik Opel Avatar answered Sep 27 '22 20:09

Henrik Opel


That sounds like a custom job to me. You can use the Form API in your module to make form. Look at pretty much any other module to see examples of how Form API works. Then, you'll want to create a $node object from all of the values of those fields and use node_submit() and node_save() to actually create the different nodes.

I'd suggest maybe looking at the Webform module for using the Form API and hijacking the submit process to make it create those two node types.

like image 36
theunraveler Avatar answered Sep 27 '22 19:09

theunraveler


Option #3, using one of the youngish solutions in creating a CCK nodereference field that can point to a non-existing node, and create it on submit.

Node Reference Create looks like one of the more stable of these projects.

Node Reference Auto-create and Node Reference Field appear to have more value-added for determining values in the new node.

This has a secondary advantage in building a nodereference between the modules, which you can use to integrate the nodes when rendered, create Views, and more.

like image 21
Grayside Avatar answered Sep 27 '22 21:09

Grayside