0

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

0

I found the solution. It seems values from fields inside the field_collections are not managed as other Drupal form values.

This is the code to achieve the result:

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'),
    );
    $form['field_test_collection']['und'][0]['surround_fieldset']['field_collection_first'] = $form['field_test_collection']['und'][0]['field_collection_first'];
    unset($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'];
    unset($form['field_test_collection']['und'][0]['field_collection_second']);
    $form['#submit'][] = 'mymodule_node_form_submit';
}

function mymodule_node_form_submit($form, &form_state) {
    $form_state['field']['field_test_collection']['und']['entity'][0]->field_collection_first['und'][0]['value']=$form_state['values']['field_test_collection']['und'][0]['surround_fieldset']['field_collection_first']['und'][0]['value']; 
    $form_state['field']['field_test_collection']['und']['entity'][0]->field_collection_second['und'][0]['value']=$form_state['values']['field_test_collection']['und'][0]['surround_fieldset']['field_collection_second']['und'][0]['value'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.