Hide a field collection field in an embedded form

460 views Asked by At

I am trying to hide a field collection field that renders in an embedded form. This form renders on a few different nodes but only some nodes need all the fields, so I would like to hide them on ones that don't.

Right now I am trying to do it vis hook form alter but it doesn't work inside the field collection, it will work on a form rendered normally that shares fields with the collection.

function zenstrap_form_alter(&$form,&$form_state,$form_id){

    //Form we want to target
    case ($form_id==="coh_pow_node_form"):

    //Appears in Normal form and Field Collection
    //Hides in normal
    $form['field_last_name']['#access']=FALSE;

   //Appears in Normal form and Field Collection
   //Hides in normal
   $form['field_street']['#access']=FALSE;

   //Appears in Field Collection
   //Does nothing
   $form['field_veteran_retired']['#access']=FALSE;

   break;

}
1

There are 1 answers

0
PraveenKumar On

To Hide field collection fields check the code below.

function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'YOURFORMID') {
        $delta = 0;
        $max_delta = $form['field_YOUR_field_collection'][LANGUAGE_NONE]['#max_delta'];
        while ($delta <= $max_delta) {
            $form['field_YOUR_field_collection'][LANGUAGE_NONE][$delta]['field_YOURfield'][LANGUAGE_NONE][0]['#access'] = FALSE;
            $delta++;
        }
    }
}

Hope it helps you...