I have recipe node thats using field collection for the ingredients. Each ingredient field collection has about 8 fields (we are using it to store other info too) and its set to unlimited so I can get a "Add more items" button. This all works just fine.
What I need to do, though, is create a way to "clone" a row... so not just add another row, but also pre-populate that row with the data stored in another row. I was hoping to just use some jquery to drop in a simple button and just copy the HTML with modified IDs and drop in the values, but it looks like thats not going to work.
I'm hoping to get some direction on how best to proceed!
thanks,
Ron
Comments
Comment #1
tim.plunkettComment #2
guillaumev commentedI'm looking for a way to do that. So far, I was able to add some rows in hook_form_FORM_ID_alter by taking the first element and copying it, but the issue is that, even though the fields appear on the form, when saving the form, only the last element is saved, so there is an issue with this method...
Comment #3
sarasioux commentedI also need this feature. Does anyone have any existing work arounds, or is there some timeline around when this will make it into the primary module roadmap?
Basically a "clone" button on a field collection, right next to the "Edit" & "Delete" buttons, which trigger the same functionality as "Add Another Item" except the item would be pre-populated with the same field data.
Someone post a patch and I will help with testing!
Comment #4
loze commented+1 for this.
In my use case I do not need a "clone" button, but I am loading data from another source and need to be able to prepopulate multiple rows of my field collection with this data on the edit form.
I am running into the same issue as #2 with only the last item being saved.
any ideas?
Comment #5
johnpitcairn commentedI have an interest in this functionality. I know my clients will want it.
Comment #6
achilles085 commentedany updates on this?
Comment #7
colinmcclure commented+1
I could really do with this functionality on a project I am on right now as well.
Comment #8
stephencross commentedAnyone aware of progress of this?
-S
Comment #9
aaronbaumanI don't mind using a form_alter to add rows, but doing so is causing fatal errors that I cannot track down.
I need this kind of feature to pre-populate some field collections on the node/add form.
Can anybody give an outline of how to approach this?
Comment #10
hjulien commentedThere's a module that might work for this use case called at https://www.drupal.org/project/field_collection_node_clone
This is what it says it does:
This module generates a cloned field collection so that each node has its own collection.
Another option is maybe to put the rows into a table? Here: https://www.drupal.org/project/field_collection_table.
See if either of these work for you.
Comment #11
aaronbaumanfield_collection_node_clone extends node_clone to support field collections, so that's not really what i want.
field_collection_table sounds like it implies a very specific form layout, which won't work for me either.
Here's how I ended up solving this in a form_alter:
Comment #12
joelstein commentedThis patch adds the ability to clone Field Collection items. You have to first edit the field settings (on the referencing entity) and configure the field to enable cloning.
Then you'll find a "Clone" button next to the "Remove" button. It will clone the current item and add it to the bottom of the item list.
It also takes care to clone nested Field Collection fields. So, even though the use case may be a little crazy and the interface can get cluttered, it's possible to clone an item within an item that itself can also be cloned.
When cloning, brand new Field Collection items are created.
Please test it out! It's working well for me with many common field types.
Comment #13
mikemike10212003 commentedHi,
I use the patch file of Joelstein.
However, I flush all cache and then it will return HTTP ERROR 500.
I click "manage field" of some content type which contains field collection, and it also return HTTP ERROR 500.
I view those nodes and it will appear "Notice: Undefined index: clone in field_collection_field_widget_form()"
Could anybody fix it?
Comment #14
carsonwThe 500 error mentioned in 13 is due to:
PHP Fatal error: Cannot redeclare field_collection_update_7007()I upped the number to 7009 and all is well.
Comment #15
ram4nd commentedIn that case this one needs a re-roll.
Comment #16
ram4nd commentedComment #17
Prabhat Kumar Tiwari commentedAdded reroll for 7.x-1.x-dev version.
Comment #18
Prabhat Kumar Tiwari commentedHello,
I have updated the patch file of joelstein and done re-roll.
Still getting error in " field_collection.test " file.
Could anyone fix it?