Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save orakili/3718568 to your computer and use it in GitHub Desktop.
Save orakili/3718568 to your computer and use it in GitHub Desktop.
Drupal 7 - Views 3.5 - Patch for #1024832
diff --git a/modules/taxonomy/views_handler_relationship_node_term_data.inc b/modules/taxonomy/views_handler_relationship_node_term_data.inc
index d7fbb4c..7046cfb 100644
--- a/modules/taxonomy/views_handler_relationship_node_term_data.inc
+++ b/modules/taxonomy/views_handler_relationship_node_term_data.inc
@@ -54,10 +54,12 @@ class views_handler_relationship_node_term_data extends views_handler_relationsh
function query() {
$this->ensure_my_table();
- $def = $this->definition;
+ $vocabularies = array_filter($this->options['vocabularies']);
+
+ $def = array();
$def['table'] = 'taxonomy_term_data';
- if (!array_filter($this->options['vocabularies'])) {
+ if (empty($vocabularies) || !empty($this->options['required'])) {
$taxonomy_index = $this->query->add_table('taxonomy_index', $this->relationship);
$def['left_table'] = $taxonomy_index;
$def['left_field'] = 'tid';
@@ -65,16 +67,16 @@ class views_handler_relationship_node_term_data extends views_handler_relationsh
$def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
}
else {
- // If vocabularies are supplied join a subselect instead
+ // If vocabularies are supplied and the relationship is not required, join a subselect.
$def['left_table'] = $this->table_alias;
$def['left_field'] = 'nid';
$def['field'] = 'nid';
- $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
+ $def['type'] = 'LEFT';
$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->addJoin('INNER', 'taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
+ $query->addJoin('INNER', 'taxonomy_index', 'tn', 'tn.tid = td.tid');
+ $query->condition('tv.machine_name', $vocabularies);
$query->addTag('term_access');
$query->fields('td');
$query->fields('tn', array('nid'));
@@ -82,14 +84,17 @@ class views_handler_relationship_node_term_data extends views_handler_relationsh
}
$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);
+
+ if (!empty($vocabularies) && !empty($this->options['required'])) {
+ // If vocabularies are supplied and the relationship is required, filter by the vocabularies.
+ $vocabulary = $this->query->ensure_table('taxonomy_vocabulary', $this->alias);
+ $this->query->add_where(0, "$vocabulary.machine_name", $vocabularies);
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment