1

Here are 2 unsuccessful experiments I've done:

<?php

/**
 * My first attempt with implementing hook_form_alter.
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'MYNODETYPE_node_form':
      foreach ($form['field_MYFIELDCOLLECTION']['und'] as $delta => $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(&$element, &$form_state, $context) {
  if ($element['#field_name'] == 'field_MYFIELDCOLLECTION' && $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.

6
  • do the functions actually run? and does it make it through the if statements?
    – Geoff
    Commented Jun 15, 2014 at 20:19
  • @Geoff yes, they do. The only issue is that the new array elements I set seem to be ignored by the form API. Commented Jun 15, 2014 at 20:22
  • 1
    I took a look at a field collection I use via dpm, and it is layed out like this: $element[field][und][0]][value][default value]. Are you sure about the array structure for the fields you are trying to access?
    – Geoff
    Commented Jun 15, 2014 at 20:24
  • Nope, I am not :-D. I am trying out your suggestion. Commented Jun 15, 2014 at 20:26
  • 1
    Please be aware the "und" part should be replaced by [LANGUAGE_NONE]
    – Michiel
    Commented Jun 15, 2014 at 20:50

1 Answer 1

7

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.

<?php

/**
 * Implements hook_form_alter.
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'MYNODETYPE_node_form':
      foreach ($form['field_MYFIELDCOLLECTION']['und'] as $delta => $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(&$element, &$form_state, $context) {
  if ($element['#field_name'] == 'field_MYFIELDCOLLECTION' && $context['instance']['bundle'] == 'MYNODETYPE') {

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

  }
}
1
  • I don't know what the advantages are but you can also use form_set_value($form["field_MYFIELDCOLLECTION"][LANGUAGE_NONE][$delta]['field_SOMEFIELD'], array(LANGUAGE_NONE => array(0 => array('value' => 123346))), $form_state); at this point instead of setting the $element, but i think it does the same thing. Commented Jan 9, 2015 at 12:31

Your Answer

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

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