I have a node, inside this node I have a field_collection with one checkbox and 2 fields inside it. I want to move these two fields inside a fieldset to show/hide entire fieldset depending of the checkbox value.
function mymodule_form_testtype_node_form_alter(&$form, &$form_state, $form_id) {
$form['field_test_collection']['und'][0]['surround_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Sorround fieldset'),
'#tree' => FALSE,
);
$form['field_test_collection']['#tree'] = FALSE;
$form['field_test_collection']['und'][0]['surround_fieldset']['field_collection_first'] = $form['field_test_collection']['und'][0]['field_collection_first'];
$form['field_test_collection']['und'][0]['surround_fieldset']['field_collection_second'] = $form['field_test_collection']['und'][0]['field_collection_second'];
}
But when I try to submit the form these two values are not saved. I tried to remove the original fields using
unset($form['field_test_collection']['und'][0]['field_collection_first']);
or hide them with
$form['field_test_collection']['und'][0]['field_collection_first']['#access'] = 0;
I also tried the "cheat" to move back these two fields in the validate function
function mymodule_article_form_testtype_node_form_validate(&$form, &$form_state) {
$form_state['values']['field_test_collection']['und'][0]['field_collection_first'] = $form_state['values']['field_test_collection']['und'][0]['surround_fieldset']['field_collection_first'];
$form_state['values']['field_test_collection']['und'][0]['field_collection_second'] = $form_state['values']['field_test_collection']['und'][0]['surround_fieldset']['field_collection_second'];
unset($form_state['values']['field_test_collection']['und'][0]['surround_fieldset']);
$form['field_test_collection']['und'][0]['field_collection_first'] = $form['field_test_collection']['und'][0]['surround_fieldset']['field_collection_first'];
$form['field_test_collection']['und'][0]['field_collection_second'] = $form['field_test_collection']['und'][0]['surround_fieldset']['field_collection_second'];
unset($form['field_test_collection']['und'][0]['surround_fieldset']);
$form['field_test_collection']['und'][0]['field_collection_first']['#access'] = 1;
$form['field_test_collection']['und'][0]['field_collection_second']['#access'] = 1;
}
With a dpm($form_state) in the submit function I can see correct values in their places, but after it they're not saved.
Any idea? Thank you