Download & Extend

Alter Field with hook_form_alter

Project:Field collection
Version:7.x-1.0-beta5
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

Hey guys,

I try to determine the "number of values" of a field collection via hook_form_alter.
With normal fields (e.g. textfield) this works without problems.

With a field collection the number of field items, let´s say two, displays correct, the problem is that just the last value is saved.
If I re-edit the node, agian, both items are displayed with a default_value of the last field.

Here´s the code.

Any help is really appreciated.

function mymodule_form_alter(&$form, &$form_state, $form_id){
    if($form_id == 'node_edit_form'){
       
        //Get the first item
        $new_item = $form['field_field_collection']['und'][0];
       
        //Simply set the first item as second item
        $form['field_field_collection']['und'][1] = $new_item;
       
        //Setting the delta, which maybe is a good idea, results in a crash (Malform exeption)
        #$form['field_field_collection']['und'][1]['#delta'] = 1;
       
        //The following code doesn´t have an effect. Just a try...
        //Tell the second items´ field "Field Col Test", which is a textfield, its parent is item two.
        $form['field_field_collection']['und'][1]['field_col_test']['und'][0]['#field_parents'][2] = 1;
    }
}

Comments

#1

no one?

#2

nobody click here