I have code that creates a simple node with a few text boxes and a select box. I want to use this prototype for an Estimate Content Type where each record has header information ( name, dates, location, process description etc. ) and then multiple costing lines ( e.g component, manufacturing stage, material cost, labour cost etc ). I have tested this out using the UI Content Type creation form and I have used the View Field View module to create a nice looking View which gives the estimate header data and then multiple estimate line items. I now want to use a custom form rather than the standard Content Type form.

Can someone point me a some sample code that shows how the form module code needs to change to include a field collection widget allowing multiple entries for each group of fields in the collection ? Thanks.

Comments

jfurnas’s picture

Are you creating the form programatically, or are you creating the form via the forms api and the ui? If you are doing it programatically, it's pretty complex and I am currently working through the same type of thing.

What you will need to do is keep an index of the total number of 'repeatable fields' you have on the form, and use ajax to create an ajax submit handler to increase this number by (x) amount every time a 'add new item' button is clicked (The same way the multi-field UI widget works). You then loop through the number of items to display and create a new field element based on that number, which you then return in the hook_form() hook.

peterk900’s picture

Yes from the research I have carried out I can understand that this is complex territory. My aim was to use the Form API to create the form, replacing the auto generated content type form, and then to use a UI created Field Collection. I am still wondering if this is possible.

If I have understood you correctly your project seems to be to provide code to create both the form and the field collection which is rather more ambitious that my plan, and I'd need to up my Drupal coding skills, particularly with Ajax, before tackling this. Thanks for describing the code plan and I wish you well with this work. I hope to read more about how you progress in this forum.

peterk900’s picture

Yes from the research I have carried out I can understand that this is complex territory. My aim was to use the Form API to create the form, replacing the auto generated content type form, and then to use a UI created Field Collection. I am still wondering if this is possible.

If I have understood you correctly your project seems to be to provide code to create both the form and the field collection which is rather more ambitious that my plan, and I'd need to up my Drupal coding skills, particularly with Ajax, before tackling this. Thanks for describing the code plan and I wish you well with this work. I hope to read more about how you progress in this forum.

jfurnas’s picture

I could possibly help you in my free-time if it's something that you completely want custom. If it's beyond your capability and aren't looking for help, it may be best to stick with the UI if it currently does what you need it to. Sometimes making a content-type is overkill as it has a lot of unneeded stuff, so it may be more beneficial to install the ECK (Entity Construction Kit), CTools, and the Entity module (Including the entity UI) that would let you create your own entities (e.g. node) and bundles (e.g. article), without all of the other stuff (You can still field them, but the overhead will only be the fields you give it).

If you want more help on that, let me know.

peterk900’s picture

..both for this post and also for the post via my contact form ( I could read what you wrote from the email but for some reason that I haven't sorted I can't access this via Drupal.org ). Help would be appreciated - as you say in your post picking up the fine detail of how the Drupal API works is a definite challenge. I need to free up some time as Drupal work is currently an R & D project to move away from Joomla/Fabrik for forms-intensive web application development. Once I have cracked how the forum personal posts system works, perhaps I can make contact again when I am clear of some of my non-Drupal work ? Thanks again for your offer to help which is much appreciated.