diff --git a/forms/FieldSet.php b/forms/FieldSet.php index f37d91e58..0f686b14c 100755 --- a/forms/FieldSet.php +++ b/forms/FieldSet.php @@ -112,23 +112,25 @@ class FieldSet extends DataObjectSet { * This function will create any missing tabs. * @param array $fields An array of {@link FormField} objects. */ - public function addFieldsToTab($tabName, $fields) { + public function addFieldsToTab($tabName, $fields, $insertBefore = null) { $this->flushFieldsCache(); // Find the tab $tab = $this->findOrMakeTab($tabName); - + // Add the fields to the end of this set foreach($fields as $field) { // Check if a field by the same name exists in this tab - if($tab->fieldByName($field->Name())) { + if($insertBefore) { + $tab->insertBefore($field, $insertBefore); + } elseif($tab->fieldByName($field->Name())) { // It exists, so we need to replace the old one $this->replaceField($field->Name(), $field); } else { $tab->push($field); } } - } + } /** * Remove the given field from the given tab in the field.