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 :)
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | webform-current-component-value-2199523.patch | 1.2 KB | chalk |
Comments
Comment #1
chalk commentedComment #2
chalk commentedComment #3
quicksketchAround line 2250 in webform_client_form(), the $input_values variable is assembled like this:
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.
Comment #4
quicksketchAh! I see what the problem is. Your patch is pretty much right on, but we can simplify it.
This line (currently 2283),
Should simply be:
That should use the values set at the beginning of the webform_client_form() function.
Comment #5
chalk commented@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.
Comment #6
quicksketchSorry, 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.
$form_state['values']['submitted'] should also be keyed by $cid, after the submit handlers have fired. They're converted in this line:
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?
Comment #7
chalk commented@quicksketch I found a solution for my problem.
Sorry for my panic. Thanks a lot for your help with code example!
Comment #8
chalk commented