function _wfm_values_prepare in Webform Multiple (WFM) 7
Sanitize the $form_state so that it can be processed by Webform.
In $form_state['submitted']['values'], Webform expects to get an array like:
array(
$parent_key => array(
$component_key => $value,
),
);
for every component.
The value in $value above can be either scalar or a single-dimensional array.
1 call to _wfm_values_prepare()
- wfm_validate in ./
wfm.module - Validation callback for the webform, running before Webform callbacks.
File
- ./
wfm.module, line 445 - Main module file for Webform Multiple (WFM).
Code
function _wfm_values_prepare(&$form_state, $components) {
// Remove all non-component values, except 'details' and 'op' because they are
// needed later by webform_client_form_pages()
$details = $form_state['values']['details'];
if (isset($form_state['values']['op'])) {
$op = $form_state['values']['op'];
}
form_state_values_clean($form_state);
$form_state['values']['details'] = $details;
if (isset($op)) {
$form_state['values']['op'] = $op;
}
// Build a list of component IDs keyed by their 'form_key' string properties.
$cids_by_key = array();
foreach ($components as $cid => $component) {
$cids_by_key[$component['form_key']] = $cid;
}
$component_ancestors = $form_state['wfm_ancestors'];
// Go through all the non-group components, getting their values and
// identifying where they should have extra identification added describing
// the hierarchy of their parents.
foreach ($components as $cid => $component) {
$form_key = $component['form_key'];
// Only act on non-group components.
if (webform_component_feature($component['type'], 'group')) {
continue;
}
// Check whether the component, or any of its ancestors, is multiple-value.
$is_multiple_recursive = _wfm_is_multiple_recursive($component, $components);
if (!$is_multiple_recursive) {
continue;
}
// Loop through all the possible family trees for this component.
if (!isset($component_ancestors[$cid])) {
$child_element = $components[$cid];
$ancestors = array(
$child_element['form_key'],
);
$ancestors_key = $child_element['form_key'];
while ($child_element['pid'] != 0) {
$child_element = $components[$child_element['pid']];
array_unshift($ancestors, $child_element['form_key']);
$ancestors_key = $child_element['form_key'] . '.' . $ancestors_key;
}
array_unshift($ancestors, 'submitted');
$ancestors_key = 'submitted.' . $ancestors_key;
$component_ancestors[$cid][$ancestors_key] = $ancestors;
}
foreach ($component_ancestors[$cid] as $ancestors) {
// Remove 'submitted' from the ancestors array.
array_shift($ancestors);
// Get the component's value.
$value = drupal_array_get_nested_value($form_state['values']['submitted'], $ancestors, $set);
if (!$set) {
continue;
}
// Take the value out of $form_state.
_wfm_array_unset_nested_value($form_state['values']['submitted'], $ancestors);
// Recursively check if the value is empty (equivalent to 0), and if so
// stop processing.
$value = _wfm_filter_recursive($value);
if (!$value) {
continue;
}
// Make sure the value is an array;
$value = (array) $value;
// If this component is multiple-value, or if it's a descendant of any
// multiple-value components, then prefix each of its array keys with a
// string describing the component's ancestors. The array keys are used in
// the database column {webform_submitted_data}.`no`.
$ancestors_count = count($ancestors);
if ($ancestors_count > 1 && $is_multiple_recursive) {
$delta_prefix = '';
// Each array element will be prefixed by something like '1|2#0|3#0|4',
// where the delta keys are the numbers preceded by hash signs, and the
// other integers are component IDs.
foreach ($ancestors as $key => $ancestor) {
if ($key < $ancestors_count) {
if ($key > 0) {
$delta_prefix .= is_int($ancestor) ? '#' : '|';
}
// If the ancestor is a form key, convert it to a component ID.
if (!is_int($ancestor)) {
$ancestor = $cids_by_key[$ancestor];
}
$delta_prefix .= $ancestor;
}
}
$value_prefixed = array();
foreach ($value as $delta => $sub_value) {
$new_delta = $delta_prefix . '#' . $delta;
$value_prefixed[$new_delta] = $sub_value;
}
$value = $value_prefixed;
}
// Remove deltas from parents of this value.
$value_parents = array_filter($ancestors, 'is_string');
// Set the value again in $form_state.
foreach ($value as $key => $sub_value) {
$sub_value_parents = $value_parents;
$sub_value_parents[] = $key;
drupal_array_set_nested_value($form_state['values']['submitted'], $sub_value_parents, $sub_value, TRUE);
}
}
}
// Remove empty arrays from $form_state.
$form_state['values']['submitted'] = _wfm_filter_recursive($form_state['values']['submitted']);
}