Index: nodereference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/nodereference.module,v
retrieving revision 1.39.2.35
diff -u -p -r1.39.2.35 nodereference.module
--- nodereference.module	24 Mar 2008 01:58:34 -0000	1.39.2.35
+++ nodereference.module	31 Mar 2008 22:14:10 -0000
@@ -104,6 +104,80 @@ function nodereference_field_settings($o
           'extra' => array('field' => $field),
         ),
       );
+      
+  case 'tables':
+      $tables = content_views_field_tables($field);
+      // Add another copy of the field tabe, joining on the referenced
+      $tables['node_data_'. $field['field_name'].'_rev'] = array(
+        'name' => $tables['node_data_'. $field['field_name']]['name'],
+        'join' => array(
+          'left' => array(
+            'table' => 'node',
+            'field' => 'nid',
+          ),
+          'right' => array(
+            'field' => $field['field_name'].'_nid',
+          ),
+        ),
+      );
+      return $tables;
+
+    case 'arguments':
+      $arguments = content_views_field_arguments($field);
+      $field_types = _content_field_types();
+      $arguments['content: '. $field['field_name'] .'_rev'] = array(
+        'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' rev ('. $field['field_name'] .')',
+        'handler' => 'noderef_views_argument_handler',
+      );
+      return $arguments;
+  }
+}
+
+function noderef_views_argument_handler($op, &$query, $argtype, $arg = '') {
+  if ($op == 'filter') {
+    $field_name = substr($argtype['type'], 9, -4);
+  }
+  else {
+    $field_name = substr($argtype, 9, -4);
+  }
+  $field = content_fields($field_name);
+
+  // The table name used here is the Views alias for the table, not the actual
+  // table name.
+  $table = 'node_data_'. $field['field_name'].'_rev';
+
+  switch ($op) {
+    case 'summary':
+      // To produce summaries we need to join to the referrer nid, rather than the referenced nid
+      $joininfo = array(
+        'left' => array(
+          'table' => 'node',
+          'field' => 'nid',
+        ),
+        'right' => array(
+          'field' => 'nid',
+        ),
+      );
+      $query->add_table($table, FALSE, 1, $joininfo);
+      $query->add_field("title");
+      $query->add_field('nid', $table);
+      $fieldinfo['field'] = "$table.nid";
+      return $fieldinfo;
+
+    case 'sort':
+      break;
+
+    case 'filter':
+      $nid = intval($arg);
+      $query->ensure_table($table);
+      $query->add_where("$table.nid = %d", $nid);
+      break;
+
+    case 'link':
+        return l($query->title, "$arg/$query->nid");
+
+    case 'title':
+      return content_format($field, $item, 'plain');
   }
 }
 
