Hi,

I have created: #2153723: Form_state values in _webform_render_component implementations. But we haven't found resolve.

I have checked Webform 4.x API - feature for my module Webform Multiple File isn't implemented.

The idea of feature is delivering data from $form_state to _webform_render_component() function during AJAX rebuilding (for example). In current version $value in _webform_render_component() is available only for already submitted form.

I have investigated that the problem is in form and form state keys.

During webform build in _webform_client_form_add_component() the key is $component['form_key']:

$parent_fieldset[$component['form_key']] = $display_element; 

In webform_client_form() there is variable $input_values that stores data from saved webform submission and $form_state. Next step is:

$component_value = isset($input_values[$cid]) ? $input_values[$cid] : NULL;

As you can see, the key is $component['cid']. But correct values for component in $form_state are keyed as $component['form_key'] during AJAX rebuild. $component_value seems to be set only during editing already submitted webform, isn't it?

Also I found that keys for $form_state values are changed during webform submit in function webform_client_form_pages():

  // Flatten trees within the submission.
  $form_state['values']['submitted'] = _webform_client_form_submit_flatten($node, $form_state['values']['submitted']);

So.. I can't find solution how to delivery values to my _webform_render_component() during AJAX form rebuild. My proposal is attached patch that allow to use both form state and submitted values.

Thanks for attention. I hope that we can solve this problem :)

Comments

chalk’s picture

chalk’s picture

Issue summary: View changes
quicksketch’s picture

$component_value seems to be set only during editing already submitted webform, isn't it?

Around line 2250 in webform_client_form(), the $input_values variable is assembled like this:


    // Set the input values based on whether we're editing an existing
    // submission or not.
    $input_values = isset($submission->data) ? $submission->data : array();

    // Form state storage override any default submission information. Convert
    // the value structure to always be an array, matching $submission->data.
    if (isset($form_state['storage']['submitted'])) {
      foreach ($form_state['storage']['submitted'] as $cid => $data) {
        $input_values[$cid] = is_array($data) ? $data : array($data);
      }
    }

    // Form state values override any default submission information. Convert
    // the value structure to always be an array, matching $submission->data.
    if (isset($form_state['values']['submitted'])) {
      foreach ($form_state['values']['submitted'] as $cid => $data) {
        $input_values[$cid] = is_array($data) ? $data : array($data);
      }
    }

So in short, $input_values is going to be 1) the current values from $form_state['values'] 2) the values from $form_state['storage'] if coming from previous pages of the form or finally 3) the $submission object if editing an existing submission.

Since you need the values from $form_state['values'], those should be set as the $input_values already. File upload handling can get a little weird however, so perhaps you're right that the values aren't getting handled like I expect.

quicksketch’s picture

Ah! I see what the problem is. Your patch is pretty much right on, but we can simplify it.

This line (currently 2283),

      $component_value = isset($form_state['values']['submitted'][$cid]) ? $form_state['values']['submitted'][$cid] : NULL;

Should simply be:

      $component_value = isset($input_values[$cid]) ? $input_values[$cid] : NULL;

That should use the values set at the beginning of the webform_client_form() function.

chalk’s picture

@quicksketch I'm happy that we found understanding :)

I wrote this patch for last version of 7.x-4.x version, not for release 7.x-4.0-beta3. There's some differences in code: in 7.x-4.0-beta3 _webform_client_form_add_component() function variable $component_value is unused.

About simplifying. I want to note that both keys $component['cid'] and $component['form_key'] may exist in $input_values array. Values in $input_values from $submission are keyed as 'cid', and values from $form_state are keyed as 'form_key'. And my patch allows use both these values, but $form_state values has more priority than $submission.

quicksketch’s picture

Sorry, I've confused myself. In #4, I was looking at the 3.x branch, not the 4.x branch. It looks to me like everything is already the way it should be in 4.x.

Values in $input_values from $submission are keyed as 'cid', and values from $form_state are keyed as 'form_key'.

$form_state['values']['submitted'] should also be keyed by $cid, after the submit handlers have fired. They're converted in this line:

  // Flatten trees within the submission.
  $form_state['values']['submitted'] = _webform_client_form_submit_flatten($node, $form_state['values']['submitted']);

So things in $form_state are keyed by form key, but only for a short time and (in theory) never during the form-building phase. So I can't figure out why your patch would be necessary. Have you tried the latest 4.x version with your module?

chalk’s picture

@quicksketch I found a solution for my problem.
Sorry for my panic. Thanks a lot for your help with code example!

chalk’s picture

Status: Needs review » Closed (works as designed)