Using #states with field collection I'm trying to use #states in a unlimited field collection (using the field collection module). 

I'm adding this functionality to a node add form so am altering form elements using hook_form_alter.

I can get it to work for the first item in the collection using the following code but it doesn't work for items added subsequently.

$form[$k]['field_employee_name']['#states'] = array(
    'visible' =&gt; array(
        ':input[name="field_witnesses[und][0][field_person_type][und]"]' =&gt; array('value' =&gt; 'Employee'),
    ),
);
To get this to work we need to add the #states functionality to every field collection after the ajax callback has taken place.

To do this add an #after_build function to the field collection item in hook_form_alter:

$form['field_witnesses'][LANGUAGE_NONE]['#after_build'][] = 'field_witnesses_after_build';


And then in the after build function loop over the elements to add the #states behavior:

function field_witnesses_after_build($form, &amp;$form_state) {
    foreach($form as $k =&gt; $v) {
        if(isset($v['#entity_type']) &amp;&amp; $v['#entity_type']=='field_collection_item') {

            $form[$k]['field_employee_name']['#states'] = array(
                'visible' =&gt; array(
                    ':input[name="field_witnesses[und]['.$k.'][field_person_type][und]"]' =&gt; array('value' =&gt; 'Moto Employee'),
                ),
            );

        }
    }
    return $form;
}