function content_multigroup_node_form_transpose_elements in Content Construction Kit (CCK) 6.3
Transpose element positions in $form_state for the fields in a multigroup.
1 call to content_multigroup_node_form_transpose_elements()
- _content_multigroup_node_form_after_build in modules/
content_multigroup/ content_multigroup.node_form.inc - Fix form and posting data when the form is submitted.
File
- modules/
content_multigroup/ content_multigroup.node_form.inc, line 482 - Implementation of node edit functions for content multigroup.
Code
function content_multigroup_node_form_transpose_elements(&$form, &$form_state, $type_name, $group_name) {
$groups = fieldgroup_groups($type_name);
$group = $groups[$group_name];
$group_fields = $form['#multigroups'][$group_name];
// Save the remove state of multigroup items in the $form_state array.
if (!isset($form_state['multigroup_removed'])) {
$form_state['multigroup_removed'] = array();
}
if (!isset($form_state['multigroup_removed'][$group_name])) {
$form_state['multigroup_removed'][$group_name] = array();
}
// Move group data from group->delta->field to field->delta.
$group_data = array();
foreach ($form_state['values'][$group_name] as $delta => $items) {
// Skip 'add more' button.
if (!is_array($items) || !isset($items['_weight'])) {
continue;
}
foreach ($group_fields as $field_name => $field) {
if (!isset($group_data[$field_name])) {
$group_data[$field_name] = array();
}
// Get field weight and remove state from the group and keep track of the
// current delta for each field item.
$item_defaults = array(
'_weight' => $items['_weight'],
'_remove' => $items['_remove'],
'_old_delta' => $delta,
);
$group_data[$field_name][$delta] = is_array($items[$field_name]) ? array_merge($items[$field_name], $item_defaults) : $item_defaults;
// Store the remove state and the element weight in the form element as
// well, so we can restore them later.
// See content_multigroup_fix_multivalue_fields().
// See content_multigroup_fix_element_values().
$form[$group_name][$delta][$field_name]['#_weight'] = $items['_weight'];
$form[$group_name][$delta][$field_name]['#removed'] = $items['_remove'];
// Insert an element valitation callback of our own at the end of the
// list to ensure the drag'n'drop weight of the element is not lost by
// a form_set_value() operation made by the validation callback of the
// widget element.
if (!isset($form[$group_name][$delta][$field_name]['#element_validate'])) {
$form[$group_name][$delta][$field_name]['#element_validate'] = array();
}
$form[$group_name][$delta][$field_name]['#element_validate'][] = 'content_multigroup_fix_element_values';
}
$form_state['multigroup_removed'][$group_name][$delta] = $items['_remove'];
}
$form_group_sorted = FALSE;
foreach ($group_data as $field_name => $items) {
// Sort field items according to drag-n-drop reordering. Deltas are also
// rebuilt to start counting from 0 to n. Note that since all fields in the
// group share the same weight, their deltas remain in sync.
usort($items, '_content_sort_items_helper');
// Now we need to apply the same ordering to the form elements. Also,
// note that deltas have changed during the sort operation, so we need
// to reflect this delta conversion in the form.
if (!$form_group_sorted) {
$form_group_items = array();
$form_deltas = array();
foreach ($items as $new_delta => $item) {
$form_deltas[$item['_old_delta']] = $new_delta;
$form_group_items[$new_delta] = $form[$group_name][$item['_old_delta']];
unset($form[$group_name][$item['_old_delta']]);
}
foreach ($form_group_items as $new_delta => $form_group_item) {
$form[$group_name][$new_delta] = $form_group_item;
}
content_multigroup_node_form_fix_deltas($form[$group_name], $form_deltas);
$form_group_sorted = TRUE;
}
// Get rid of the old delta value.
foreach (array_keys($items) as $delta) {
unset($items[$delta]['_old_delta']);
}
// Fix field and delta positions in the $_POST array.
if (!empty($_POST)) {
$_POST[$field_name] = array();
foreach ($items as $new_delta => $item) {
$_POST[$field_name][$new_delta] = $item;
}
if (isset($_POST[$group_name])) {
unset($_POST[$group_name]);
}
}
// Move field items back to their original positions.
$form_state['values'][$field_name] = $items;
}
// Finally, get rid of the group data in form values.
unset($form_state['values'][$group_name]);
}