--- /Users/james/Downloads/nodereference_field.1/nodereference_field.module	2009-06-12 20:58:18.000000000 +0100
+++ /Users/james/Sites/torchbox-csp-specifics/modules/Hacked/nodereference_field/nodereference_field.module	2009-11-19 14:55:19.000000000 +0000
@@ -103,7 +103,9 @@
         // Prefix foreign field name with 'nodereference_field_' to prevent conflict
         // in the case of self-reference.
         $field_name_foreign = $field_name;
-        $field_name_local = 'nodereference_field_' . $field_name_foreign;
+        // HACK
+        //$field_name_local = 'nodereference_field_' . $field_name_foreign;
+        $field_name_local = 'nodereference_field_' . $row_key . '_' . $field_name_foreign;
 
         // Initialize local field array if not set. All referenced
         // values for this field will be stored in this array.
@@ -117,7 +119,9 @@
         // multiple references to foreign fields.
         $value_local = &$node->$field_name_local;
         $value_foreign = &$referenced_node->$field_name_foreign;
-        $value_local[$row_key] = $value_foreign[0];
+        // HACK 
+        // $value_local[$row_key] = $value_foreign[0];
+        $value_local = $value_foreign;
       }
     }
   }
@@ -152,7 +156,9 @@
           // Prefix foreign field name with 'nodereference_field_' to prevent conflict
           // in the case of self-reference.
           $field_name_foreign = $field_name;
-          $field_name_local = 'nodereference_field_' . $field_name_foreign;
+          // HACK
+          //$field_name_local = 'nodereference_field_' . $field_name_foreign;
+          $field_name_local = 'nodereference_field_' . $row_key . '_'. $field_name_foreign;
 
           // Load the entire field definition from the foreign node into the
           // local node the first time through. After that just load the
@@ -194,9 +200,59 @@
         }
       }
     }
-  }
+  } 
+//  // HACK
+//  // This will replace the validation for elements inside auto-create
+//  // fieldsets with a wrapper function.
+//  
+//  if ($form['#id'] == 'node-form') {
+//    // examine form elements
+//    foreach($form as $form_index => $element) {
+//      // identify auto-create fieldset containers
+//      if (is_array($element) && $element['#theme'] == 'nodereference_field_node_edit') {
+//        // identify children
+//        foreach(element_children($element) as $child_index) {
+//            
+//          $child_element =& $form[$form_index][$child_index];
+//
+//          // for each instance of field, replace validation with wrapper 
+//          // and store some info for the validation wrapper
+//          foreach($child_element as $row_index => $value) {
+//            if (is_int($row_index)) {
+//              $child_element[$row_index]['#old_element_validate'] = $child_element[$row_index]['#element_validate'];    
+//              $child_element[$row_index]['#element_validate'] = array('nodereference_field_wrapped_validate');
+//              $child_element[$row_index]['#node_ref_row_index'] = $row_index;
+//              $child_element[$row_index]['#node_ref_id'] = str_replace('_container', '', $form_index);
+//            }
+//          }          
+//        }
+//      }
+//    }  
+//  } // END HACK
 }
 
+// HACK
+/**
+ * this wraps the standard validation for form elements inside an auto-create fieldset with a check
+ * to see if it needs to be run. it will run the standard validation only if required. i.e. if
+ * a new auto-create node is going to be created.
+ * 
+ * @param array $element
+ * @param array $form_state
+ */
+function nodereference_field_wrapped_validate($element, &$form_state) {
+  $added_node_ref_value = $element['#post'][$element['#node_ref_id']][$element['#node_ref_row_index']]['nid']['nid'];
+
+  if (!empty($added_node_ref_value)) {
+    if (!empty($element['#old_element_validate'])) {
+      
+      foreach($element['#old_element_validate'] as $callback) {
+        call_user_func($callback, $element, $form_state);
+      }
+    }
+  }
+}
+// END HACK
 /**
  * Add referenced fields and place inside fieldset.
  *
@@ -246,6 +302,7 @@
   $type = nodereference_autocreate_type_get($field['referenceable_types']);
   $referenced_type = content_types($type);
   $referenced_fields = $referenced_type['fields'];
+  
 
   // On node creation there are no values set for the field. Add a
   // blank value for the nodereference_autocreate field so that the
@@ -265,7 +322,7 @@
     $path = &$form[$container][$field['field_name']][$field['field_name'] . '_add_more']['#ahah']['path'];
     $path = str_replace('content/js_add_more/', 'nodereference_field/js_add_more/', $path);
   }
-
+  
   foreach ($form['#node']->$field['field_name'] as $row_key => $value) {
     foreach ($referenceable_fields as $field_name => $referenceable_field) {
       $field = $referenced_fields[$field_name];
@@ -273,7 +330,9 @@
         // Prefix foreign field name with 'nodereference_field_' to prevent conflict
         // in the case of self-reference.
         $field_name_foreign = $field_name;
-        $field_name_local = 'nodereference_field_' . $field_name_foreign;
+        // HACK
+        //$field_name_local = 'nodereference_field_' . $field_name_foreign;
+        $field_name_local = 'nodereference_field_' . $row_key .'_'.$field_name_foreign;
 
         // Maintain $field definition for content_field_form() by editing name
         // after it has been placed in #field_info.
@@ -288,14 +347,17 @@
 
         // Shift the value at the $row_key to the first position so it
         // is used by cck when the field loads the default value.
-        $temp_value = &$node->$field_name_foreign;
-        $store = $temp_value[0];
-        $temp_value[0] = $temp_value[$row_key];
+        // HACK
+//        $temp_value = &$node->$field_name_foreign;
+//        $store = $temp_value[0];
+//        $temp_value[0] = $temp_value[$row_key];
 
+        
+        
         // Load the field form element.
-        $form_element = (array) content_field_form($form, $form_state, $field);
+        $form_element = (array) content_field_form($form, $form_state, $field);               
         $form_element = (array) $form_element[$field_name];
-
+                       
         // Revert all value modification.
         $node->$field_name_foreign = $temp;
 
@@ -308,7 +370,14 @@
         // Use the weight assigned to the referenceable_field instead
         // of the weight related to the foreign node.
         $form_element['#weight'] = $referenceable_field['weight'];
-
+        
+        // HACK
+        $form_element['#old_element_validate'] = $form_element['#element_validate'];    
+        $form_element['#element_validate'] = array('nodereference_field_wrapped_validate');
+        $form_element['#node_ref_row_index'] = $row_key;
+        $form_element['#node_ref_id'] = str_replace('_container', '', $container);
+        // END HACK
+        
         // Add the field element to the container. The first time the
         // field is added to the container use the whole $form_element.
         // The second time only place the specific field for the
@@ -316,9 +385,10 @@
         if (!isset($form[$container][$field_name_local])) {
           $form[$container][$field_name_local] = $form_element;
         }
-        else {
-          $form[$container][$field_name_local][$row_key] = $form_element[0];
-        }
+        // HACK
+//        else {
+//          $form[$container][$field_name_local][$row_key] = $form_element[0];
+//        }
       }
     }
   }
@@ -352,17 +422,23 @@
               // Prefix foreign field name with 'nodereference_field_' to prevent conflict
               // in the case of self-reference.
               $field_name_foreign = $field_name;
-              $field_name_local = 'nodereference_field_' . $field_name_foreign;
+              $field_name_local = 'nodereference_field_' . $row_key . '_' .$field_name_foreign;
 
               // In case of a multiple value nodereference field assign the
               // current value related to the current referenced node given by
               // the NID at nodereference[$key] to the first value on the foreign
               // node. This assumes that the foreign node does not have a
-              // multiple value field being referenced.
-              $referenced_node->$field_name_foreign = array($form_state['values'][$field_name_local][$row_key]);
+              // multiple value field being referenced.    
+
+              //$referenced_node->$field_name_foreign = array($form_state['values'][$field_name_local][$row_key]);
+                       
+              // HACK -- if there are multiple values, add all of them
+              // note, i think this may have just fixed multiple values for single rows of autocreated fields. Can't test
+              // properly until issue with dropdown/checkbox widget not displaying on multiple auto create field.
+              $referenced_node->$field_name_foreign = $form_state['values'][$field_name_local];
+              // END HACK
             }
           }
-
           node_save($referenced_node);
         }
       }
@@ -444,13 +520,15 @@
 //        'label' => t('Body'),
 //      ),
 //    );
-
     foreach ($type['fields'] as $field) {
-      if ($field['type'] == 'nodereference' || $field['multiple'] >= 1) {
+      // HACK -- allow noderef and multiple fields
+      //if ($field['type'] == 'nodereference' || $field['multiple'] >= 1) {
+      //if ($field['multiple'] >= 1) {
         // Prevent infinite references and references to multiple value fields.
-        continue;
-      }
-
+        //continue;
+      //}
+      // END HACK
+      
       $row = array();
 
       $row['label'] = array(
