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

tim.plunkett’s picture

Category: support » feature
guillaumev’s picture

I'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...

sarasioux’s picture

I 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!

loze’s picture

+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?

johnpitcairn’s picture

I have an interest in this functionality. I know my clients will want it.

achilles085’s picture

any updates on this?

colinmcclure’s picture

+1

I could really do with this functionality on a project I am on right now as well.

stephencross’s picture

Issue summary: View changes

Anyone aware of progress of this?

-S

aaronbauman’s picture

I 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?

hjulien’s picture

There'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.

aaronbauman’s picture

field_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:

  // I need a specific cardinality for this field, which changes from entity-to-entity.
  // Just changing the cardinality doesn't seem to work, so we end up with this complicated scheme here.
  // We'll end up using field_default_form() to re-build this form field entirely, so here we prep the variables.
      $fieldname = 'field_example';
      $field = field_info_field($fieldname);
  // The field defaults to unlimited cardinality, so any integer here will pass validation:
      $field['cardinality'] = example_get_count();
      $instance = $form[$fieldname][$lang][0]['#instance'];
      $instance['entity_type'] = 'field_collection';
      $instance['bundle'] = 'field_collection_item';
      $field_collection_entity = clone $form[$fieldname][$lang][0]['#entity'];
  // Once we've mocked the right params, field_default_form will do the rest.
      $new_field = field_default_form('field_collection_item', $field_collection_entity, $field, $instance, $lang, array(), $element, $form_state);
      $element[$fieldname] = $new_field[$fieldname];
joelstein’s picture

This 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.

mikemike10212003’s picture

Hi,
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?

carsonw’s picture

The 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.

ram4nd’s picture

Status: Needs review » Needs work

In that case this one needs a re-roll.

ram4nd’s picture

Issue tags: +Novice
Prabhat Kumar Tiwari’s picture

Status: Needs work » Needs review
StatusFileSize
new4.87 KB

Added reroll for 7.x-1.x-dev version.

Prabhat Kumar Tiwari’s picture

Status: Needs review » Needs work

Hello,
I have updated the patch file of joelstein and done re-roll.
Still getting error in " field_collection.test " file.
Could anyone fix it?