2

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 !

6
  • 2
    Well, nice that you are doing it, but what is your question?
    – Mołot
    Commented Oct 18, 2013 at 12:07
  • You're calling prepopulate_form($submitted, $value_array) from my_module_form_alter but in that function $value_array is undefined.
    – Beebee
    Commented Oct 18, 2013 at 12:38
  • Molot, I have changed the question now. The form is not getting pre-populated.
    – Gokul N K
    Commented Oct 18, 2013 at 13:13
  • @Beebee I am dpm ing the $value variable and I am getting the desired output there. So that doesn't seem to be the issue.
    – Gokul N K
    Commented Oct 18, 2013 at 13:13
  • @GokulNK I think you need to pass the key only to the default value, can you give a try to this: $form[$key]['#default_value'] = key($value);. Commented Oct 21, 2013 at 9:54

1 Answer 1

4
+50

The main problem is you are not passing the form array by reference so your values are never coming back into the form. Try this (note the ampersand on $form['submitted']):

function my_module_form_alter(&$form, &$form_state, $form_id) {
  $submitted = &$form['submitted'];
  prepopulate_form($submitted, $value_array);
}

Otherwise you are only modifying the $submitted variable and not the actual form.

Alternatively, you could also just pass $form['submitted'] into the prepopulate_form() function instead of using the $submitted variable and that would also work.

Plus, you are using $value_array, which does not exist, so you will need to pass actual values in.

2
  • Thanks for the inputs, I will check it out. By the way I am passing proper values, was using $value_array just to simplify the code while pasting here:)
    – Gokul N K
    Commented Oct 21, 2013 at 10:13
  • That worked like a charm. I completely overlooked that part. Since $form was being passed by reference, I thought that would suffice. Thanks for the answer.
    – Gokul N K
    Commented Oct 21, 2013 at 10:27

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.