I am trying to pre-populate a webform based on the values from a third party service.
I am using form_alter to start the processing and then using a recursive function to traverse the tree. I am passing the sub-form(part of a form of type fieldset) by reference, so that changes do carry forward.
Here is the code :
function my_module_form_alter(&$form, &$form_state, $form_id) {
$submitted = $form['submitted'];
prepopulate_form($submitted, $value_array);
}
function prepopulate_form(&$form, $value_array) {
foreach ($form as $key => $settings) {
if (is_array($settings) && isset($settings['#type'])) {
if ($settings['#type'] == 'fieldset') {
prepopulate_form($form[$key], $value_array);
}
$value = 'Some logic to get the value from value arrya';
dpm($value); // I am getting the expected output here.
$form[$key]['#default_value'] = $value; // The changes I make here are not being carried to the actual $form array I suppose
}
}
}
But the form is not getting pre-populated. I am not sure what I am missing here.
Any insights would help. Thanks !
prepopulate_form($submitted, $value_array)
frommy_module_form_alter
but in that function$value_array
is undefined.key
only to the default value, can you give a try to this:$form[$key]['#default_value'] = key($value);
.