How to form_alter fields attached to an unlimited field collection? Here are 2 unsuccessful experiments I've done:

&lt;?php

/**
 * My first attempt with implementing hook_form_alter.
 */
function MYMODULE_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch ($form_id) {
    case 'MYNODETYPE_node_form':
      foreach ($form['field_MYFIELDCOLLECTION']['und'] as $delta =&gt; $field) {
        if (is_numeric($delta)) {

          // None of these work:
          $form['field_MYFIELDCOLLECTION']['und'][$delta]['field_SOMEFIELD']['und']['#default_value'] = 'test';
          $form['field_MYFIELDCOLLECTION']['und'][$delta]['field_SOMEFIELD']['#default_value'] = 'test';

          $form['field_MYFIELDCOLLECTION']['und'][$delta]['field_SOMEFIELD']['und']['#value'] = 'test';
          $form['field_MYFIELDCOLLECTION']['und'][$delta]['field_SOMEFIELD']['#value'] = 'test';

        }
      }
  }
}


/**
 * My second attempt implementing hook_field_widget_form_alter.
 */
function MYMODULE_field_widget_form_alter(&amp;$element, &amp;$form_state, $context) {
  if ($element['#field_name'] == 'field_MYFIELDCOLLECTION' &amp;&amp; $context['instance']['bundle'] == 'MYNODETYPE') {
    // These don't work either:
    $element['field_SOMEFIELD']['#value'] = 'test';
    $element['field_SOMEFIELD']['#default_value'] = 'test';

    $element['field_SOMEFIELD']['und']['#value'] = 'test';
    $element['field_SOMEFIELD']['und']['#default_value'] = 'test';
  }
}


How can I alter the form API elements on fields attached to a field collection?

PS: The hook functions are running fine. The issue is that the form API values I set do not make any change on the loaded form.
Check the array to make sure you are using the correct names and structure.  You can use dpm() from the devel module to check this.

Both hook_form_alter or hook_field_widget_form_alter allow you to alter field collection child field form elements.

&lt;?php

/**
 * Implements hook_form_alter.
 */
function MYMODULE_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch ($form_id) {
    case 'MYNODETYPE_node_form':
      foreach ($form['field_MYFIELDCOLLECTION']['und'] as $delta =&gt; $field) {
        if (is_numeric($delta)) {

          $form['field_MYFIELDCOLLECTION']['und'][$delta]['field_SOMEFIELD']['und'][0]['value']['#default_value'] = 'default value';
          $form['field_MYFIELDCOLLECTION']['und'][$delta]['field_SOMEFIELD']['und'][0]['value']['#value'] = 'value';

        }
      }
  }
}


The following will also work:

/**
 * Implements hook_field_widget_form_alter.
 */
function MYMODULE_field_widget_form_alter(&amp;$element, &amp;$form_state, $context) {
  if ($element['#field_name'] == 'field_MYFIELDCOLLECTION' &amp;&amp; $context['instance']['bundle'] == 'MYNODETYPE') {

    $element['field_SOMEFIELD']['und'][0]['value']['#value'] = 'value';
    $element['field_SOMEFIELD']['und'][0]['value']['#default_value'] = 'default value';

  }
}