hey guys,

I'm implementing a custom submit handler (for a n entityform) and running into wall with handling field collection items. I got everything in a wrapper (entity_metadata_wrapper), and everything else insert ok. Went through a quite a few posts online with not much success.

Entiyform has field collection item field_movie
Field collection item field_movie has
- year
- name
- studio

Thanks.

Comments

j-phat created an issue. See original summary.

j-phat’s picture

Issue summary: View changes
hondaman900’s picture

Did you find a solution and, if so, can you share code? I too am having difficulty saving new data to an existing FC. I use a FC in an entityform, and list the multiple entityforms and FC contents as rows in my own module custom form. Only the last FC in in the group (wrapper) is saved and the prior FCs are ignored. Looking for ages and can't find any takers to weigh in on what I'm doing wrong.

j-phat’s picture

i had this happen before, only to realize i hadn't changed the field names. if that's not the case, try dumping the field values to make sure they have the correct values just before submit. hope that helps.

hondaman900’s picture

Thanks for the reply j-phat.

Not sure what you mean by "change the field names". In my foreach loop I cycle through the multiple field collection fields in an entityform (because the entityform allows for multiple fc's) and the field I'm changing is the same name in each fc. I change the names of the form fields to match the correct form field with the correct FC field. I do a dsm() dump to check if my set() statement is working and I see all the correct values set, but the last wrapper->save() statement only saves the last fc.

Here's the code:

function investment_lineitems_form_submit($form, &$form_state) {

    $entityform = array_pop(arg());  // entityform ID passed in URL argument

    $wrapper = entity_metadata_wrapper('entityform', $entityform); // fetch specific entityform

    $collection_count = 0; //use this couter to read back numbered fields in form

    //cycle through each line in the field collection
    foreach ($wrapper->field_inv_account_details as $field_collection_wrapper) {   

        $collection_count++;  //increment the counter on each loop - first one is 1

        $collection_item_value = $form_state['input']['edit_your_percent_amount_' . $collection_count];  //grab value from form field
        dsm("collection item value: ".$collection_count." = ".$collection_item_value); //check to see if we're getting the correct value - we are

        $field_collection_wrapper->field_inv_your_percent_split_a->set($collection_item_value);  // set new value in the field collection field
        dsm($field_collection_wrapper->field_inv_your_percent_split_a->value()); //check that we've set the correct value - we have
    }

    //the following should save all items in the field collection
    // this should save the field collection, but only the last field collection item is being written back to the database
    $field_collection_wrapper->save();  
    
    drupal_set_message(t('The form has been submitted.'));
} 
j-phat’s picture

Hondaman, try saving the fieldset inside the foreach loop.

hondaman900’s picture

Wow! Of course, that works and explains why I was only getting the last one saved - I couldn't see it. It works fine now.

Thank you SO much. I've lost over a week trolling forums ad posts and posting this everywhere trying to figure this perplexing issue out. You saved my life on this one!