You are here

function _content_multigroup_node_form_after_build in Content Construction Kit (CCK) 6.3

Fix form and posting data when the form is submitted.

FormAPI uses form_builder() during form processing to map incoming $_POST data to the proper elements in the form. It builds the '#parents' array, copies the $_POST array to the '#post' member of all form elements, and it also builds the $form_state['values'] array. Then the '#after_build' hook is invoked to allow custom processing of the form structure, and that happens just before validation and submit handlers are executed.

During hook_form_alter(), the multigroup module altered the form structure moving elements from field->delta to multigroup->delta->field position, which is what has been processed by FormAPI to build the form structures, but field validation (and submit) handlers expect their data to be located in their original positions.

We now need to move the fields back to their original positions in the form, and we need to do so without altering the form rendering process, which is now reflecting the structure the multigroup is interested in. We just need to fix the parts of the form that affect validation and submit processing.

1 call to _content_multigroup_node_form_after_build()
content_multigroup_node_form_after_build in modules/content_multigroup/content_multigroup.module
After build callback for multigroups in node form.

File

modules/content_multigroup/content_multigroup.node_form.inc, line 311
Implementation of node edit functions for content multigroup.

Code

function _content_multigroup_node_form_after_build($form, &$form_state) {

  // Disable required flag during FormAPI validation, except when building the
  // form for an 'Add more values' request.
  $required = !empty($form_state['multigroup_add_more']);
  foreach ($form['#multigroups'] as $group_name => $group_fields) {
    if (!empty($form['#multigroup_required_fields'][$group_name])) {
      $required_fields = array_keys($form['#multigroup_required_fields'][$group_name]);
      content_multigroup_node_form_fix_required($form[$group_name], $required_fields, $required);
    }
  }
  if ($form_state['submitted'] && !$form_state['multigroup_add_more']) {

    // Fix value positions in $form_state for the fields in multigroups.
    foreach (array_keys($form['#multigroups']) as $group_name) {
      content_multigroup_node_form_transpose_elements($form, $form_state, $form['#node']->type, $group_name);
    }

    // Fix form element parents for all fields in multigroups.
    content_multigroup_node_form_fix_parents($form, $form['#multigroups']);

    // Update posting data to reflect delta changes in the form structure.
    if (!empty($_POST)) {
      content_multigroup_node_form_fix_post($form);
    }
  }
  return $form;
}