Problem
After update to webform 7.x-4.14 I found a php Notice was being always displayed after submit a webform.
Digging in the code I found the problem was in the way the function webform_submission_render is excluding components.
// Remove excluded components.
if (is_array($excluded_components)) {
foreach ($excluded_components as $cid) {
unset($components[$cid]);
}
if (!empty($email['exclude_empty'])) {
foreach ($submission->data as $cid => $data) {
// Caution. Grids store their data in an array index by question key.
if (implode($data) == '') {
unset($components[$cid]);
}
}
}
}
When $data is a matrix, the implode will return an error (we only can implode strings not arrays).
Solution
Take into account the $data variable could be a multidimensional array. The previous module version code will work fine.
// Remove excluded components.
if (is_array($excluded_components)) {
foreach ($excluded_components as $cid) {
unset($components[$cid]);
}
if (!empty($email['exclude_empty'])) {
foreach ($submission->data as $cid => $data) {
// Caution. Grids store their data in an array index by question key.
foreach ($data as $value) {
if ($value != '') {
// This component has a non-empty value. Continue the outer loop.
continue 2;
}
}
unset($components[$cid]);
}
}
}
I know it's not the most efficient approach but I think we should roll back to the previous version to avoid problem and then think in a better way to do it.
There is another issue that maybe related but it was closed recently , #2601546: Improve "exclude_empty" processing.. The approach in that ticket is use implode, so the problem will still be there.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | webform_submission.exclude_empty-2832175-15.patch | 621 bytes | fran seva |
| #7 | Selection_132.png | 106.91 KB | fran seva |
| #6 | webform_submission.exclude_empty-2832175-6.patch | 794 bytes | fran seva |
Comments
Comment #2
fran seva commentedI created a patch that revert the code to the previous version and fix the problem.
Comment #3
fran seva commentedComment #5
liam morlandDo I understand correctly that you see this every time a form is submitted if it contains a grid component?
Comment #6
fran seva commentedI have recreated the previous patch.
Comment #7
fran seva commentedHi @liam-morland,
The way I have to reproduce the error is use addressfield module but the error will happen everytime the implode receive a grid.
I'm thinking how create a test to reproduce the error without use addressfield. I'm not sure how to mock this use case.
Comment #8
fran seva commentedComment #9
liam morlandComment #10
liam morlandYour patch seems to reverse #2601546: Improve "exclude_empty" processing.. We need a solution that doesn't cause that problem to come back. Maybe _webform_client_form_submit_flatten() can be used.
Comment #11
liam morlandComment #12
fran seva commentedComment #13
fran seva commentedHi -- I've been working on it and I'm not sure if we can use _webform_client_form_submit_flatten() or _webform_components_tree_flatten.
Looking in Drupal core, I found we could use options_array_flatten function that flat multidimensional arrays without take into account any special key component as previously functions do.
What do you think about it? Do you think _webform_client_form_submit_flatten should work?
Comment #15
fran seva commentedSorry, I created the patch using the wrong branch.
Comment #16
fran seva commentedComment #17
liam morlandThanks for the patch. Can you write a test for this?
Comment #18
fran seva commentedComment #19
igorski commentedLiam, maybe I can write a test. Can you elaborate on what’s needed?
Comment #20
liam morlandIt would be great to have a test that would submit a test form containing the sort of component that would cause this problem. The test would fail without this patch.
Comment #21
liam morlandDrupal 7 is no longer supported. If this applies to a supported version, please re-open.