You are here

function content_multigroup_node_form_fix_parents in Content Construction Kit (CCK) 6.3

Fix form element parents for all fields in multigroups.

The $element['#parents'] array needs to reflect the position of the fields in the $form_state['values'] array so that form_set_value() can be safely used by field validation handlers.

1 call to content_multigroup_node_form_fix_parents()
_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 611
Implementation of node edit functions for content multigroup.

Code

function content_multigroup_node_form_fix_parents(&$elements, $multigroups) {
  foreach (element_children($elements) as $key) {
    if (isset($elements[$key]) && $elements[$key]) {

      // Check if the current element is child of a multigroup. The #parents
      // array for field values has, at least, 3 parent elements, being the
      // first one the name of a multigroup.
      if (count($elements[$key]['#parents']) >= 3 && isset($multigroups[$elements[$key]['#parents'][0]])) {

        // Extract group name, delta and field name from the #parents array.
        array_shift($elements[$key]['#parents']);
        $delta = array_shift($elements[$key]['#parents']);
        $field_name = array_shift($elements[$key]['#parents']);

        // Now, insert field name and delta to the #parents array.
        array_unshift($elements[$key]['#parents'], $field_name, $delta);
      }

      // Recurse through all children elements.
      content_multigroup_node_form_fix_parents($elements[$key], $multigroups);
    }
  }
}