--- content.module	2009-03-21 22:31:57.000000000 +0100
+++ content.module	2009-03-21 22:30:28.000000000 +0100
@@ -422,6 +422,7 @@ function content_nodeapi(&$node, $op, $a
     if ($op == 'view') {
       $node->content['#pre_render'][] = 'content_alter_extra_weights';
       $node->content['#content_extra_fields'] = $type['extra'];
+      $node->content['#content_type'] = $node->type;
     }
   }
 }
@@ -439,6 +440,7 @@ function content_form_alter(&$form, $for
     }
     $form['#pre_render'][] = 'content_alter_extra_weights';
     $form['#content_extra_fields'] = $type['extra'];
+    $form['#content_type'] = $form['#node']->type;
   }
 }
 
@@ -451,12 +453,31 @@ function content_alter_extra_weights($el
       // Some core 'fields' use a different key in node forms and in 'view'
       // render arrays. Check we're not on a form first.
       if (!isset($elements['#build_id']) && isset($value['view']) && isset($elements[$value['view']])) {
-        $elements[$value['view']]['#weight'] = $value['weight'];
+        $elements[$value['view']]['#weight'] = $value['weight'] - 1000;
       }
       elseif (isset($elements[$key])) {
-        $elements[$key]['#weight'] = $value['weight'];
+        $elements[$key]['#weight'] = $value['weight'] - 1000;
       }
     }
+
+    if (module_exists('fieldgroup')) {
+      foreach (array_keys(fieldgroup_groups($elements['#content_type'])) as $group_name) {
+        if (isset($elements[$group_name]) && isset($elements[$group_name]['#weight'])) {
+          $elements[$group_name]['#weight'] -= 1000;
+        }
+      }
+    }
+    $content_type = content_types($elements['#content_type']);
+    foreach (array_keys($content_type['fields']) as $field_name) {
+      if (isset($elements[$field_name]) && isset($elements[$field_name]['#weight'])) {
+        $elements[$group_name]['#weight'] -= 1000;
+      }
+    }
+
+    // Make sure this process is not executed more than once. Note that the
+    // node form is rendered twice. Once from FAPI and a second time from
+    // theme_node_form() in node.pages.inc.
+    unset($elements['#content_extra_fields']);
   }
   return $elements;
 }
