1 views_handler_relationship_node_term_data.inc views_handler_relationship_node_term_data::query()

Called to implement a relationship in a query.

Overrides views_handler_relationship::query

File

core/modules/taxonomy/views/views_handler_relationship_node_term_data.inc, line 54
Definition of views_handler_relationship_node_term_data.

Class

views_handler_relationship_node_term_data
Relationship handler to return the taxonomy terms of nodes.

Code

function query() {
  $this->ensure_my_table();

  $def = $this->definition;
  $def['table'] = 'taxonomy_term_data';

  if (!array_filter($this->options['vocabularies'])) {
    $taxonomy_index = $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_index', 'tn', 'tn.tid = td.tid');
    $query->condition('td.vocabulary', array_filter($this->options['vocabularies']));
    if (empty($this->query->options['disable_sql_rewrite'])) {
      $query->addTag('taxonomy_term_access');
    }
    $query->fields('td');
    $query->fields('tn', array('nid'));
    $def['table formula'] = $query;
  }

  $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);
}