Index: modules/taxonomy/views_handler_relationship_node_term_data.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/modules/taxonomy/views_handler_relationship_node_term_data.inc,v
retrieving revision 1.3.4.3
diff -u -p -r1.3.4.3 views_handler_relationship_node_term_data.inc
--- modules/taxonomy/views_handler_relationship_node_term_data.inc	7 Oct 2010 19:40:02 -0000	1.3.4.3
+++ modules/taxonomy/views_handler_relationship_node_term_data.inc	12 Jan 2011 22:11:21 -0000
@@ -52,43 +52,44 @@ class views_handler_relationship_node_te
    */
   function query() {
     $this->ensure_my_table();
+    $this->query->add_table('taxonomy_index', $this->relationship);
 
     $def = $this->definition;
     $def['table'] = 'taxonomy_term_data';
-
-    if (!array_filter($this->options['vocabularies'])) {
-      $term_node = $this->query->add_table('taxonomy_index', $this->relationship);
-      $def['left_table'] = 'taxonomy_index';
-      $def['left_field'] = 'tid';
-      $def['field'] = 'tid';
-      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
-    }
-    else {
-      // If vocabularies are supplied join a subselect instead
-      $def['left_table'] = $this->table_alias;
-      $def['left_field'] = 'nid';
-      $def['field'] = 'nid';
-      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
-
-      $query = db_select('taxonomy_term_data', 'td');
-      $query->addJoin($def['type'], 'taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
-      $query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
-      $query->condition('tv.machine_name', array_filter($this->options['vocabularies']));
-      $query->addTag('term_access');
-      $query->fields('td');
-      $query->fields('tn', array('nid'));
-      $def['table formula'] = $query;
-    }
+    $def['left_table'] = 'taxonomy_index';
+    $def['left_field'] = 'tid';
+    $def['field'] = 'tid';
+    $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
 
     $join = new views_join();
-
     $join->definition = $def;
     $join->construct();
     $join->adjusted = TRUE;
 
-    // use a short alias for this:
     $alias = $def['table'] . '_' . $this->table;
-
     $this->alias = $this->query->add_relationship($alias, $join, 'taxonomy_term_data', $this->relationship);
+
+    // Add join and where clauses to query if vocabularies are defined. 
+    if ($vocabularies = array_filter($this->options['vocabularies'])) {
+      $def = array();
+      $def['table'] = 'taxonomy_vocabulary';
+      $def['left_table'] = 'taxonomy_term_data';
+      $def['left_field'] = 'vid';
+      $def['field'] = 'vid';
+      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
+
+      $join = new views_join();
+      $join->definition = $def;
+      $join->construct();
+
+      $alias = $this->query->ensure_table('taxonomy_vocabulary', $this->alias, $join);
+
+      $group = 'views_handler_relationship_node_term_data';
+      $this->query->add_where($group, "$alias.machine_name", $vocabularies);
+      if (empty($this->options['required'])) {
+        $this->query->set_where_group('OR', $group);
+        $this->query->add_where($group, "$alias.machine_name", NULL);
+      }
+    }
   }
 }
