diff --git a/filefield.module b/filefield.module index c199990..b1e50c2 100644 --- a/filefield.module +++ b/filefield.module @@ -600,11 +600,35 @@ function filefield_js($type_name, $field_name, $delta) { // Update the cached form with the new element at the right place in the form. if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) { - if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) { - $form_element = $form[$group_name][$delta][$field_name]; + //Get the fieldgroup tree information for this field + $tree = _fieldgroup_get_tree($type_name); + $parents = array(0 => $group_name); + + //Find the parent structure + while ($group_name = $tree[$group_name]['parents'][0]) { + $parents[] = $group_name; } - else { - $form_element = $form[$group_name][$field_name][$delta]; + + //Put in the correct order + $parents = array_reverse($parents); + $tmp_element = $form; + + //Now we need to find the field within the structure. So iterate down to its parents level. + foreach ($parents AS $idx) { + if (isset($tmp_element[$idx]) && is_array($tmp_element[$idx])) { + $tmp_element = $tmp_element[$idx]; + } + } + + //Now we have the element that contains the field. Lets get the field + $field = $tmp_element[$field_name]; + + //Now we have to determine what to send to the render function + if (isset($field[$delta])) { + $form_element = $field[$delta]; + } + else { //Using the field since that is what is left + $form_element = $field; } }