0

I have had no luck trying to set the value of a field in a field group. I can see in the array that the default value has changed but the change does not show up in the viewed form. This is the same whether I place the code in form_alter or after_build.

If the field is not in the field group, the changed value is visible in the viewed form.

This is the code I'm testing:

<?php

/**
* Modify CCK form elements on the example node form.
*/
function test_after_build($form, &$form_state) {

// $form['field_collection']['und'][0]['value']['#default_value'] = t('Smithfield');

 dsm($form);  // pretty print array using Krumo to messages

  return $form;
}


/**
 * Implementation of hook_form_alter().
 */
function test_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'test_record_node_form')  { 

 // target a specific form 
  drupal_set_message("Form ID is : " . $form_id); 

  $form['field_collection']['und'][0]['value']['#default_value'] = t('Smithfield');

    // Add an after_build function to process when everything's complete.
    $form['#after_build'][] = 'test_after_build'; 

   dsm($form);  // pretty print array using Krumo to messages

  } 

}
?>

I don't know what I need to add to this to make it work. Help appreciated.

0

I was able to spend some more time on this and figured out that the field group was not where I should be changing the value in the form.

I am using the field group multiple module so the value is not being set at the field or the field group. If using devel, look for something like 'fgm_node_YOURNODE_form_group_YOURGROUPNAME' where YOURNODE will be the name of your content type and YOURGROUPNAME will be the name you gave to your field group. I'm assuming 'fgm' is short for field group multiple.

You will have to travese 'fgm_node_YOURNODE_form_group_YOURGROUPNAME' to find the specific field within the field group multiple that you want to set the default value.

Example:

$form['fgm_node_YOURNODE_form_group_YOURGROUPNAME']['fields']['items'][0][field_YOURFIELD']['und'][0]['value']['#default_value'] = t('Some text');

My form alter code works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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