--- sites/all/modules/cck/modules/nodereference/nodereference.module	2009-05-26 16:27:06.000000000 -0400
+++ sites/all/modules/cck/modules/nodereference/nodereference.module	2009-05-26 17:27:44.000000000 -0400
@@ -108,6 +108,13 @@ function nodereference_field_settings($o
             '#description' => t('<p>Choose the "Views module" view that selects the nodes that can be referenced.<br />Note:</p>') .
               t('<ul><li>Only views that have fields will work for this purpose.</li><li>This will discard the "Content types" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate nodes on node creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate nodes will be displayed.</li></ul>'),
           );
+          //Gives the administrator the option to pass arguments for the advanced view through the url. 
+          $form['advanced']['advanced_url_view_args'] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Allow view arguments to be passed through URL'),
+            '#default_value' => isset($field['advanced_url_view_args']) ? $field['advanced_url_view_args'] : false,
+            '#description' => t('Arguments can be passed through the url by using the view\'s argument handlers. Use "%1" for the first argument, "%2" for the second, etc..'),
+          );
           $form['advanced']['advanced_view_args'] = array(
             '#type' => 'textfield',
             '#title' => t('View arguments'),
@@ -130,6 +137,7 @@ function nodereference_field_settings($o
       if (module_exists('views')) {
         $settings[] = 'advanced_view';
         $settings[] = 'advanced_view_args';
+        $settings[] = 'advanced_url_view_args';
       }
       return $settings;
 
@@ -600,11 +608,27 @@ function nodereference_autocomplete_proc
   // path and some extra processing to it.
   // Add a validation step where the value can be unwrapped.
   $field_key  = $element['#columns'][0];
-
+  /* 
+	 * Alter the autocomplete path to pass any url arguments. 
+   * depending on the current path, the arg() of the first argument can vary
+   * Therefore, the index $i depends on what comes first. 
+   */
+  $autopath = 'nodereference/autocomplete/'. $element['#field_name'];
+  if ($form['#field_info'][$element['#field_name']]['advanced_url_view_args']) {
+    if ((arg(0)=='node' && (arg(1)=='add' || arg(2)=='edit')) || (arg(0)=='nodereference' && arg(1)=='autocomplete')) {    
+      $i=3;      
+    } elseif (arg(0)=='content' && arg(1)=='js_add_more') {
+      $i=4;      
+    }
+    while (arg($i)!=false) {
+      $autopath .= '/'.arg($i);
+      $i++;
+    }
+  }
   $element[$field_key] = array(
     '#type' => 'text_textfield',
     '#default_value' => isset($element['#value']) ? $element['#value'] : '',
-    '#autocomplete_path' => 'nodereference/autocomplete/'. $element['#field_name'],
+    '#autocomplete_path' => $autopath,
     // The following values were set by the content module and need
     // to be passed down to the nested element.
     '#title' => $element['#title'],
@@ -812,6 +836,32 @@ function _nodereference_potential_refere
     if (!empty($field['advanced_view_args'])) {
       // TODO: Support Tokens using token.module ?
       $view_args = array_map('trim', explode(',', $field['advanced_view_args']));
+    /*  
+     *	Allow for dynamic arguments for views by passing the url args as arguments when creating/editing nodes
+     *	In the settings page, use %n, where n is an integer denoting it's place in the url for extra arguments
+     *  This works for adding and editing nodes: node/add/content_type/%1/%2/.... and node/nid/edit/%1/%2/....
+     * 	For example, if the url is http://www.example.com/node/add/content_type/foo/bar, and the views setting is 
+     * 	'%1,%2', then foo and bar will be passed to the first and second arguments of the view, respectively. 
+     */
+      if ($field['advanced_url_view_args']) {
+        foreach ($view_args as &$argument) {
+          if (substr($argument,0,1)=='%' && substr($argument,1)>=1 && is_numeric(substr($argument,1))) {
+            $number = (int)substr($argument,1);
+            if (arg(0)=='node' && (arg(1)=='add' || arg(2)=='edit')) {
+              if (arg($number+2)!=false)
+                $argument = arg($number+2);
+              else
+                $argument = null;
+            }
+            if (arg(0)=='nodereference' && arg(1)=='autocomplete'){
+              if ($field['url_arguments'][$number]!=NULL)
+                $argument = $field['url_arguments'][$number];
+              else
+                $argument = null;
+            }
+          }
+        }
+      }
     }
     else {
       $view_args = array();
@@ -904,7 +954,16 @@ function nodereference_autocomplete($fie
   $field = $fields[$field_name];
   $match = isset($field['widget']['autocomplete_match']) ? $field['widget']['autocomplete_match'] : 'contains';
   $matches = array();
-
+  /*  
+   * Prepares the url arguments to be passed into _nodereference_potential_references(), if applicable.
+   * Also gets the autocomplete $string from the arguments as well. 
+   */
+  if ($field['advanced_url_view_args']) {
+    $url_arguments = func_get_args();
+    unset($url_arguments[0]);
+    $string = array_pop($url_arguments);
+    $field['url_arguments'] = $url_arguments; 
+  }
   $references = _nodereference_potential_references($field, $string, $match, array(), 10);
   foreach ($references as $id => $row) {
     // Add a class wrapper for a few required CSS overrides.
--- sites/all/modules/cck/includes/content.node_form.inc	2009-05-26 16:01:10.000000000 -0400
+++ sites/all/modules/cck/includes/content.node_form.inc	2009-05-26 16:11:43.000000000 -0400
@@ -217,6 +217,26 @@ function content_multiple_value_form(&$f
     $content_type = content_types($field['type_name']);
     $field_name_css = str_replace('_', '-', $field_name);
 
+    /* 
+     * Changes the #ahah path so that arguments are passed
+     * Arguments must be passed through every callback
+     */    
+    $ahahpath = 'content/js_add_more/'. $content_type['url_str'] .'/'. $field_name;    
+    if ($field['advanced_url_view_args']) {   
+      if ($field['type']=='nodereference') {
+        if ((arg(0)=='node' && (arg(1)=='add' || arg(2)=='edit')) || (arg(0)=='nodereference' && arg(1)=='autocomplete')) {    
+          $i=3;      
+        } elseif (arg(0)=='content' && arg(1)=='js_add_more') {
+          $i=4;      
+        }
+        while (arg($i)!=false) {
+          $ahahpath .= '/'.arg($i);
+          $i++;
+        } 
+      }
+    }
+
+
     $form_element[$field_name .'_add_more'] = array(
       '#type' => 'submit',
       '#name' => $field_name .'_add_more',
@@ -227,7 +247,7 @@ function content_multiple_value_form(&$f
       // including this file. Therefore, call a proxy function to do this.
       '#submit' => array('content_add_more_submit_proxy'),
       '#ahah' => array(
-        'path' => 'content/js_add_more/'. $content_type['url_str'] .'/'. $field_name,
+        'path' => $ahahpath,
         'wrapper' => $field_name_css .'-items',
         'method' => 'replace',
         'effect' => 'fade',

