Problem/Motivation
When validation rules block the submission of a webform, tables where rows and columns have NOT been switched will not display correctly: their row-titles go missing.
I think I identified the function in webform_table_element.module that writes arrays from which such tables are rendered. Problem is that the variable ($val) originally storing row-titles is being reused for storing POST data. So if there _are_ POST data, row-titles are lost.
Proposed resolution
I pass the string stored in $val to another variable and have it read again from here. (Sorry, I am not familiar with the Drupal patch syntax yet, so I am sharing my modifications below with "//÷" marking.) The hacked module seems to work fine for me.
function _table_element_expand_rows(&$element, $children) {
$rows = $element['#table_element_rows'];
$elkey = $element['#webform_component']['form_key'];
foreach ($rows as $rowkey => $val) {
//empty(0) returns TRUE...
if (!empty($rowkey) || $rowkey === 0) {
$rt=$val; //÷+ preserve $val in a new variable ($rt) so it can be written back intact as the row title after submit
foreach ($children as $child) {
$i = 0;
//form_builder renders elements without cids, so give a temp cid...
if (!isset($element[$child]['#webform_component']['cid'])) {
$cid = $i++ . 'a';
}
else {
$cid = $element[$child]['#webform_component']['cid'];
}
if (!is_null($element['#children_values'])) {
if ($element[$child]['#type'] == 'date') {
$element['#children_values'][$rowkey][$child] = webform_date_array($element['#children_values'][$rowkey][$child]);
$element[$child]['#value'] = $element['#children_values'][$rowkey][$child];
$element[$child]['#default_value'] = $element['#children_values'][$rowkey][$child];
if (!isset($element[$child]['#attributes'])) {
$element[$child]['#attributes'] = array();
}
$element[$child] = webform_expand_date($element[$child]);
}
$element[$child]['#value'] = isset($element['#children_values'][$rowkey][$child]) ? $element['#children_values'][$rowkey][$child] : '';
$element[$child]['#default_value'] = isset($element['#children_values'][$rowkey][$child]) ? $element['#children_values'][$rowkey][$child] : '';
}
$element["row__{$elkey}__{$rowkey}"]["row__{$elkey}__{$rowkey}__{$cid}"] = $element[$child];
$element["row__{$elkey}__{$rowkey}"]["row__{$elkey}__{$rowkey}__{$cid}"]['#title'] = "";
$element["row__{$elkey}__{$rowkey}"]["row__{$elkey}__{$rowkey}__{$cid}"]['#error_title'] = $element[$child]['#title'];
$element["row__{$elkey}__{$rowkey}"]["row__{$elkey}__{$rowkey}__{$cid}"]['#name'] = "row__{$elkey}__{$rowkey}__{$cid}";
if (isset($_POST["row__{$elkey}__{$rowkey}__{$cid}"])) {
if (is_array($_POST["row__{$elkey}__{$rowkey}__{$cid}"])) {
// needed for checkboxes
$val = key($_POST["row__{$elkey}__{$rowkey}__{$cid}"]);
}
else {
$val = $_POST["row__{$elkey}__{$rowkey}__{$cid}"];
}
$element["row__{$elkey}__{$rowkey}"]["row__{$elkey}__{$rowkey}__{$cid}"]['#value'] = check_plain($val);
}
$element['#row_titles']["row__{$elkey}__{$rowkey}__{$cid}"] = $element[$child]['#title'];
$element['#column_titles']["row__{$elkey}__{$rowkey}__{$cid}"] = 'row-title ' . drupal_html_class($child);
}
$element["row__{$elkey}__{$rowkey}"]['#type'] = 'table_element_row';
//÷- $element["row__{$elkey}__{$rowkey}"]['#row_title'] = $val;
$element["row__{$elkey}__{$rowkey}"]['#row_title'] = $rt; //÷+ Here reading row-title from $rt
$element['#name'] = "row__{$elkey}__{$rowkey}";
}
}
}
Remaining tasks
The same function might be responsible for the issue reported by xtr4xtr3m3 because I remember having produced the same phenomenon with a previous attempt.
My proposed solution might, of course, introduce bad side effects that I cannot foresee... Maintainers please check.
Comments
Comment #1
deardagny CreditAttribution: deardagny commentedThanks Joolius! Works perfectly for me.