Index: nat.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/nat/nat.module,v
retrieving revision 1.14.2.15
diff -u -p -r1.14.2.15 nat.module
--- nat.module	30 Jun 2007 13:58:45 -0000	1.14.2.15
+++ nat.module	4 Dec 2007 00:20:03 -0000
@@ -476,6 +476,37 @@ function _nat_get_vocabularies() {
 }
 
 /**
+ * Helper function for node insert/update to process the node's taxonomy
+ * values.
+ *
+ * @param $hierarchy
+ *   The $node->taxonomy hierarchy passed from hook_nodeapi.
+ * @param $vid
+ *   The current NAT vocabulary ID we're processing.
+ *
+ * @return
+ *   An array of vocabulary IDs and terms specified in the hierarchy.
+ */
+function _nat_process_hierarchy($hierarchy, $vid) {
+  $parents = array();
+  if (is_array($hierarchy)) {
+    foreach ($hierarchy as $tid => $parent) {
+      // If parent is object, we're in a node_save. Otherwise, edit form submit.
+      // TODO: Change this when core issue http://drupal.org/node/197532 is fixed.
+      if (is_object($parent) && $parent->vid == $vid) {
+        $parents[$parent->tid] = $parent->tid;
+      }
+      elseif (isset($hierarchy[$vid])) {
+        // Grab entire hierarchy and bail out.
+        $parents = $hierarchy[$vid];
+        break;
+      }
+    }
+  }
+  return $parents;
+}
+
+/**
  * Add node titles as terms into the taxonomy system.
  * @todo Ideas are welcome to allow retaining the hierarchy for vocabularies not
  * present in the node form.
@@ -512,13 +543,8 @@ function _nat_add_terms($vids, $title, $
     unset($edit['tid']);
 
     $edit['vid'] = $vid;
-    // Save hierarchy for vocabularies also present in the node form.
-    if (isset($hierarchy[$vid])) {
-      $edit['parent'] = $hierarchy[$vid];
-    }
-    else {
-      $edit['parent'] = array();
-    }
+
+    $edit['parent'] = _nat_process_hierarchy($hierarchy, $vid);
 
     if (count($relations)) {
       $edit['relations'] = $relations[$vid];
@@ -556,12 +582,7 @@ function _nat_update_terms($terms, $titl
   }
 
   foreach ($terms as $term) {
-    if (isset($hierarchy[$term->vid])) {
-      $edit['parent'] = $hierarchy[$term->vid];
-    }
-    else {
-      $edit['parent'] = array();
-    }
+    $edit['parent'] = _nat_process_hierarchy($hierarchy, $term->vid);
 
     if (count($relations)) {
       $edit['relations'] = $relations[$term->vid];
