2

I am trying to set the #access attribute to FALSE on a field that is within an unlimited field collection - so it is hidden from users. I am relatively new to Drupal so I'm not sure how to handle this. I tried to use hook_form_alter and some basic CSS - but I couldn't get either of the solution to work properly because of the unlimited field cardinality.

Here's what worked for one row in the field collection in a custom module:

$form['field_my_field_collection']['und'][0]['field_myfield']['#access'] = FALSE;

How do you alter a specific field in a field collection with unlimited values? Thanks in advance for any help - the Drupal community is awesome.

3

2 Answers 2

6

Since Drupal 7.8 you can leverage hook_field_widget_form_alter. You can alter any field widgets provided by field_collection before the form_alter of the main form.

function my_module_field_widget_form_alter(&$element, &$form_state, $context) {
  if ("field_my_field_collection" == $element['#bundle']) {
    if ("field_my_field_collection" == $element['#field_name']) {
      $element['#access'] = FALSE;
    }
  }
// dpm(array($element, $form_state, $context), "field_name: " . $element['#field_name'] );
}
3

Thanks for your help Mohammed, but I ended up using the following code in a custom module:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'myformid') {
$delta = 0;
$max_delta = $form['field_my_field_collection'][LANGUAGE_NONE]['#max_delta'];
while ($delta <= $max_delta) {
$form['field_my_field_collection'][LANGUAGE_NONE][$delta]['field_myfield']   
[LANGUAGE_NONE][0]['#access'] = FALSE;
  $delta++;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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