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 -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	12 May 2007 12:06:07 -0000
@@ -24,7 +24,7 @@
             ),
         ),
         'sortable' => true,
-        'addlfields' => array('changed'),
+        'addlfields' => array('changed', 'nid'),
         'help' => t('Display the title of the node.'),
       ),
       'nid' => array(
@@ -64,6 +64,7 @@
         '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 @@
         '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 @@
           '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 @@
           '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.'),
       ),
@@ -396,13 +398,25 @@
 }
 
 /*
+ * Helper function for all the node field formatting helpers to get the proper
+ * nid to use.
+ */
+function _views_handler_field_node_find_nid($fielddata, $data) {
+  // if we're showing the title of another node joined against the main node
+  // we need to find the appropriate nid
+  return ($fielddata['tablename'] == 'node') ? $data->nid : $data->{$fielddata['tablename'] . '_nid'};
+}
+
+/*
  * 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_handler_field_node_find_nid($fielddata, $data);
+  return l($value, "node/$nid");
 }
 
 /*
@@ -410,22 +424,26 @@
  * 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_handler_field_node_find_nid($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_handler_field_node_find_nid($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_handler_field_node_find_nid($fielddata, $data);
+  $changed = $data->{$fielddata['tablename'] . '_changed'};
+  return $link .' '. theme('mark', node_mark($nid, $changed));
 }
 
 /*
@@ -439,7 +457,8 @@
  * 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_handler_field_node_find_nid($fielddata, $data);
+  $node = node_load($nid);
 
   if ($fielddata['handler'] == 'views_handler_field_body') {
     $teaser = FALSE;
@@ -686,7 +705,7 @@
   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 +739,7 @@
  * 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 +773,12 @@
 }
 
 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 +864,22 @@
  * display a link to view a node
  */
 function views_handler_node_view($fieldinfo, $fielddata, $value, $data) {
+  $nid = _views_handler_field_node_find_nid($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_handler_field_node_find_nid($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 +887,13 @@
  * display a link to edit a node with a destination return
  */
 function views_handler_node_edit_destination($fieldinfo, $fielddata, $value, $data) {
+  $nid = _views_handler_field_node_find_nid($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 +901,13 @@
  * display a link to delete a node
  */
 function views_handler_node_delete($fieldinfo, $fielddata, $value, $data) {
+  $nid = _views_handler_field_node_find_nid($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 +915,13 @@
  * display a link to delete a node with a destination return
  */
 function views_handler_node_delete_destination($fieldinfo, $fielddata, $value, $data) {
+  $nid = _views_handler_field_node_find_nid($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 +929,6 @@
  * Display a node's nid, which is a little bit special.
  */
 function views_handler_node_nid($fieldinfo, $fielddata, $value, $data) {
-  return $data->nid;
+  $nid = _views_handler_field_node_find_nid($fielddata, $data);
+  return $nid;
 }
