Index: views.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/views.module,v
retrieving revision 1.166.2.40
diff -u -p -r1.166.2.40 views.module
--- views.module	5 May 2007 02:40:18 -0000	1.166.2.40
+++ views.module	25 May 2007 21:09:11 -0000
@@ -2026,6 +2026,67 @@ function views_filter_validate_array($fo
   }
 }
 
+/*
+ * Helper function for views fields to get the proper nid to use.
+ */
+function views_field_get_node_id($fielddata, $data) {
+
+  //test if the field stems from another joined node table
+  if ($aliased_table = views_find_closest_table_for_field('nid', $fielddata['tablename'], 'node')) {
+    
+    // if we get as far as node, that gets special treatment.
+    if ($aliased_table == 'node') {
+      return $data->nid;
+    }
+
+    return $data->{$aliased_table . '_nid'};
+  }
+
+  //else we return the node id of the primary node table of the field's view
+  return $fielddata['prefixed'] ? $data->{$fielddata['alias_prefix'] .'node_nid'} : $data->nid;
+}
+
+/**
+ * Helper function to find the closest field of a given name by looking backwards
+ * through the views table definitions.
+ *
+ * @param $field_name
+ *  The name of the field to search for, e.g. 'nid' or 'name'
+ * @param $current_table
+ *  The name of the current table that you're at. In field handlers, i.e. $fielddata['tablename']
+ * @param $only
+ *  An optional table name to limit when looking through table aliases. 
+ *  If a module has aliased 'node' as 'foo_node', can use $only = 'node' to look at that and not 'foo_user'
+ * @return string
+ *  The table name of the closest table containg $field_name. You can usually extend this into the
+ *  views query name with $table_name . '_' . $field_name
+ */
+function views_find_closest_table_for_field($field_name, $current_table, $only = NULL) {
+
+  // make sure the cache.inc has been included and load the tables
+  views_load_cache();
+  $tables = _views_get_tables();
+  $def = $tables[$current_table];
+
+  // if $only was passed in, check it against the current table definition
+  if (!isset($only) || $only == $def['name']) {
+    
+    if (isset($def['fields'][$field_name])) {
+      return $current_table; // success, we found the field, return the table name
+    }
+  }
+  
+  // so we are either in a table that is not $only, or the field wasn't found
+  // if we've reached the leftmost node table, return NULL
+  if ($current_table ==  'node') {
+    return NULL;
+  }
+  
+  // otherwise, recurse
+  return views_find_closest_table_for_field($field_name, $def['join']['left']['table'], $only);
+} 
+
+
 /**
  * Provide a form gadget for dates.
  */
Index: modules/views_node.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/modules/views_node.inc,v
retrieving revision 1.30.2.17
diff -u -p -r1.30.2.17 views_node.inc
--- modules/views_node.inc	5 May 2007 00:54:53 -0000	1.30.2.17
+++ modules/views_node.inc	25 May 2007 21:09:12 -0000
@@ -24,7 +24,7 @@ function node_views_tables() {
             ),
         ),
         'sortable' => true,
-        'addlfields' => array('changed'),
+        'addlfields' => array('changed', 'nid'),
         'help' => t('Display the title of the node.'),
       ),
       'nid' => array(
@@ -64,6 +64,7 @@ function node_views_tables() {
         'option' => 'string',
         'notafield' => 'true',
         'help' => t("This will create a link to the node; fill the option field with the text for the link. If you want titles that link to the node, use Node: Title instead."),
+        'addlfields' => array('nid'),
       ),
       'body' => array(
         'name' => t('Node: Body'),
@@ -81,6 +82,7 @@ function node_views_tables() {
         'notafield' => TRUE,
         'option' => 'string',
         'help' => t('Display a link to view the node. Enter the text of this link into the option field; if blank the default "View" will be used.'),
+        'addlfields' => array('nid'),
       ),
       'edit' => array(
         'name' => t('Node: Edit link'),
@@ -89,7 +91,7 @@ function node_views_tables() {
           'views_handler_node_edit' => t('Return to Node')
         ),
         'notafield' => TRUE,
-        'addlfields' => array('type', 'uid'),
+        'addlfields' => array('type', 'uid', 'nid'),
         'option' => 'string',
         'help' => t('Display a link to edit the node. Enter the text of this link into the option field; if blank the default "Edit" will be used.'),
       ),
@@ -100,7 +102,7 @@ function node_views_tables() {
           'views_handler_node_delete' => t('Return To The Frontpage'),
         ),
         'notafield' => TRUE,
-        'addlfields' => array('type', 'uid'),
+        'addlfields' => array('type', 'uid', 'nid'),
         'option' => 'string',
         'help' => t('Display a link to delete the node. Enter the text of this link into the option field; if blank the default "Delete" will be used.'),
       ),
@@ -336,7 +338,6 @@ function node_views_arguments() {
 
 
 function node_views_default_views() {
-
   $view = new stdClass();
   $view->name = 'frontpage';
   $view->description = t('The basic front page view.');
@@ -399,10 +400,12 @@ function node_views_default_views() {
  * Format a field as a link to a node.
  */
 function views_handler_field_nodelink($fieldinfo, $fielddata, $value, $data) {
+  
   if ($fielddata['options'] == 'nolink') {
     return check_plain($value);
   }
-  return l($value, "node/$data->nid");
+  $nid = views_field_get_node_id($fielddata, $data);
+  return l($value, "node/$nid");
 }
 
 /*
@@ -410,22 +413,26 @@ function views_handler_field_nodelink($f
  * the option field as a link to the node.
  */
 function views_handler_field_node_link($fieldinfo, $fielddata, $value, $data) {
-  return l($fielddata['options'], "node/$data->nid");
+  $nid = views_field_get_node_id($fielddata, $data);
+  return l($fielddata['options'], "node/$nid");
 }
 
-
 /*
  * Format a field as a link to a 'mark', stating whether or not the node has
  * updated since it was last viewed by the user.
  */
 function views_handler_field_nodelink_with_mark($fieldinfo, $fielddata, $value, $data) {
+  $nid = views_field_get_node_id($fielddata, $data);
   if ($fielddata['options'] == 'nolink') {
     $link = check_plain($value);
   }
   else {
-    $link = l($value, "node/$data->nid");
+    $link = l($value, "node/$nid");
   }
-  return $link .' '. theme('mark', node_mark($data->nid, $data->node_changed));
+
+  $nid = views_field_get_node_id($fielddata, $data);
+  $changed = $data->{$fielddata['tablename'] . '_changed'};
+  return $link .' '. theme('mark', node_mark($nid, $changed));
 }
 
 /*
@@ -439,7 +446,8 @@ function views_handler_nodetype($fieldin
  * Format a field as the Body of a node.
  */
 function views_handler_field_body($fieldinfo, $fielddata, $value, $data) {
-  $node = node_load($data->nid);
+  $nid = views_field_get_node_id($fielddata, $data);
+  $node = node_load($nid);
 
   if ($fielddata['handler'] == 'views_handler_field_body') {
     $teaser = FALSE;
@@ -686,7 +694,7 @@ function views_handler_filter_isnew($op,
   if (!$user || !$user->uid) {
     return;
   }
-
+  
   // Hey, Drupal kills old history, so nodes that haven't been updated
   // since NODE_NEW_LIMIT are bzzzzzzzt outta here!
 
@@ -720,10 +728,7 @@ function views_handler_filter_nodetype($
  * Set a query to be distinct as a filter handler
  */
 function views_handler_filter_distinct($op, $filter, $filterinfo, &$query) {
-  if (!$query->no_distinct) {
-    $query->set_distinct();
-    $query->add_groupby('node.nid');
-  }
+  $query->add_groupby($filterinfo['table'] . '.nid');
 }
 
 /*
@@ -757,11 +762,12 @@ function views_handler_sort_random($acti
 }
 
 function views_node_sort_handler_nid($action, &$query, $sortinfo, $sort) {
-  $query->orderby[] = "node.nid $sort[sortorder]";
+  $field = $sortinfo['table'] . '.' . $sortinfo['field'];
+  $query->orderby[] = "$field $sort[sortorder]";
 }
 
 function views_node_query_handler_nid($fielddata, $fieldinfo) {
-  return "node.nid";
+  return $fielddata['fullname'];
 }
 
 function views_handler_arg_node_feed($op, &$query, $argtype, $arg = '') {
@@ -847,20 +853,22 @@ function views_post_view_make_args($view
  * display a link to view a node
  */
 function views_handler_node_view($fieldinfo, $fielddata, $value, $data) {
+  $nid = views_field_get_node_id($fielddata, $data);
   $link_text = $fielddata['options'] ? $fielddata['options'] : t('View');
-  return l($link_text, "node/$data->nid");
+  return l($link_text, "node/$nid");
 }
 
 /**
  * display a link to edit a node
  */
 function views_handler_node_edit($fieldinfo, $fielddata, $value, $data) {
+  $nid = views_field_get_node_id($fielddata, $data);
   // try to build a fake node object
   $data->type = $data->node_type;
   $data->uid = $data->node_uid;
   if (node_access('update', $data)) {
     $link_text = $fielddata['options'] ? $fielddata['options'] : t('Edit');
-    return l($link_text, "node/$data->nid/edit");
+    return l($link_text, "node/$nid/edit");
   }
 }
 
@@ -868,12 +876,13 @@ function views_handler_node_edit($fieldi
  * display a link to edit a node with a destination return
  */
 function views_handler_node_edit_destination($fieldinfo, $fielddata, $value, $data) {
+  $nid = views_field_get_node_id($fielddata, $data);
   // try to build a fake node object
   $data->type = $data->node_type;
   $data->uid = $data->node_uid;
   if (node_access('update', $data)) {
     $link_text = $fielddata['options'] ? $fielddata['options'] : t('Edit');
-    return l($link_text, "node/$data->nid/edit", NULL, drupal_get_destination());
+    return l($link_text, "node/$nid/edit", NULL, drupal_get_destination());
   }
 }
 
@@ -881,12 +890,13 @@ function views_handler_node_edit_destina
  * display a link to delete a node
  */
 function views_handler_node_delete($fieldinfo, $fielddata, $value, $data) {
+  $nid = views_field_get_node_id($fielddata, $data);
   // try to build a fake node object
   $data->type = $data->node_type;
   $data->uid = $data->node_uid;
   if (node_access('delete', $data)) {
     $link_text = $fielddata['options'] ? $fielddata['options'] : t('Delete');
-    return l($link_text, "node/$data->nid/delete");
+    return l($link_text, "node/$nid/delete");
   }
 }
 
@@ -894,12 +904,13 @@ function views_handler_node_delete($fiel
  * display a link to delete a node with a destination return
  */
 function views_handler_node_delete_destination($fieldinfo, $fielddata, $value, $data) {
+  $nid = views_field_get_node_id($fielddata, $data);
   // try to build a fake node object
   $data->type = $data->node_type;
   $data->uid = $data->node_uid;
   if (node_access('delete', $data)) {
     $link_text = $fielddata['options'] ? $fielddata['options'] : t('Delete');
-    return l($link_text, "node/$data->nid/delete", NULL, drupal_get_destination());
+    return l($link_text, "node/$nid/delete", NULL, drupal_get_destination());
   }
 }
 
@@ -907,5 +918,5 @@ function views_handler_node_delete_desti
  * Display a node's nid, which is a little bit special.
  */
 function views_handler_node_nid($fieldinfo, $fielddata, $value, $data) {
-  return $data->nid;
+  return views_field_get_node_id($fielddata, $data);
 }
Index: modules/views_taxonomy.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/modules/views_taxonomy.inc,v
retrieving revision 1.27.2.10
diff -u -p -r1.27.2.10 views_taxonomy.inc
--- modules/views_taxonomy.inc	5 May 2007 00:52:29 -0000	1.27.2.10
+++ modules/views_taxonomy.inc	25 May 2007 21:09:12 -0000
@@ -266,10 +266,10 @@ function views_taxonomy_process_form($el
  */
 function views_handler_field_allterms($fieldinfo, $fielddata, $value, $data) {
   if ($fieldinfo['vocabulary']) {
-    $terms = taxonomy_node_get_terms_by_vocabulary($data->nid, $fieldinfo['vocabulary']);
+    $terms = taxonomy_node_get_terms_by_vocabulary(views_field_get_node_id($fielddata, $data), $fieldinfo['vocabulary']);
   }
   else {
-    $terms = taxonomy_node_get_terms($data->nid);
+    $terms = taxonomy_node_get_terms(views_field_get_node_id($fielddata, $data));
   }
 
   if ($fielddata['options'] == 'nolink') {
