I'm trying to add a field collection to my user account on an event, and it seems to be being created properly, the fields are updated as expected and it parents it to the user account, however, even though the field collection is setup to have unlimited possible values, it only seems to keep the last value, it seems to keep overwriting the old one whenever a new field collection is added.
My code is below
global $user;
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_downloads'));
$field_collection_item->setHostEntity('user', $user);
$field_collection_item->field_subqueue[LANGUAGE_NONE][] = array('target_id' => $subqueue->subqueue_id);
$field_collection_item->field_file[LANGUAGE_NONE][] = array('fid' => $file->fid, 'display' => 1, 'description' => $filename);
$field_collection_item->save(TRUE);
field_attach_update('user', $user);
As mentioned, it appears to be creating the field collection properly, I can check in my account and it saves the file and correctly sets the entity reference for the field_subqueue
field too, but it only seems to save 1, like I say, the field is definitely set to allow unlimited values, it even shows me the button to 'add another item' if I check it in the user account directly.
Is there something I'm doing wrong?
EDIT
I really need to try to get this fixed so I'm gonna open it up to a bounty.
The full context of this code is that it's on the callback of an AJAX form that I've written, everything about the code seems to happen correctly, the form submission happens correctly, it seems to save the field collection, it just seems to overwrite the previous entry each time. It isn't running as part of a loop or anything, each time the form is submitted, it should save it using the field_attach_update
line
/**
* Submit handler for saving a new deck
*/
function my_module_form_submit($form, &$form_state){
global $user;
// Load the PHP library
libraries_load('my_library');
// Prepare an ajax command to insert the node html into our ajax wrapper.
$commands = array();
$commands['effect'] = 'fade';
if (form_get_errors() !== null) {
$message = t('You have an error');
$commands[] = ajax_command_invoke(NULL, array('firePopup'), array(json_encode($message), 'error', 3500)); // This is just a custom Javascript command I have called firePopup
}
else {
$values = $form_state['values'];
$values['my_var'] = json_decode($form_state['input']['_my_vars']);
$filename = empty($values['title']) ? date("F j Y - g-ia") : $values['title'];
// Custom function here to generate a 'subqueue' from the module 'node_queue', returns a $subqueue object
// Add the downloaded file to the field collection for the user
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_downloads'));
$field_collection_item->setHostEntity('user', $user);
$field_collection_item->field_subqueue[LANGUAGE_NONE][] = array('target_id' => $subqueue->subqueue_id);
$field_collection_item->field_file[LANGUAGE_NONE][] = array('fid' => $pptx->fid, 'display' => 1, 'description' => $filename);
$field_collection_item->save(TRUE);
field_attach_update('user', $user);
// Set the response message
$message = t('Your download is available.');
$download_link = l(t('Download'), file_create_url($field_collection_item->field_file[LANGUAGE_NONE][0]['uri']), array(
'attributes' => array(
'class' => array(
'button-type',
'download'
),
'download' => file_create_url($field_collection_item->field_file[LANGUAGE_NONE][0]['uri']))));
$commands[] = ajax_command_invoke(NULL, array('refreshView'), array('downloads'));
$commands[] = ajax_command_invoke(NULL, array('firePopup'), array(json_encode($message), 'status', 0, $download_link));
}
// Render the commands into JSON and print them.
return array('#type' => 'ajax', '#commands' => $commands);
}
If there's any variables here which look like they come out of nowhere, don't worry about that too much, it's only because I've removed some of the sub-functions and it's probably more confusing to add multiple sub-functions in too etc.
The code all appears to work fine, no errors, it just seems to overwrite the field collection each time.