You are here

public function NodeTermData::query in Views (for Drupal 7) 8.3

Called to implement a relationship in a query.

Overrides RelationshipPluginBase::query

File

lib/Views/taxonomy/Plugin/views/relationship/NodeTermData.php, line 66
Definition of Views\taxonomy\Plugin\views\relationship\NodeTermData.

Class

NodeTermData
Relationship handler to return the taxonomy terms of nodes.

Namespace

Views\taxonomy\Plugin\views\relationship

Code

public function query() {
  $this
    ->ensureMyTable();
  $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->tableAlias;
    $def['left_field'] = 'nid';
    $def['field'] = 'nid';
    $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
    $def['adjusted'] = TRUE;
    $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;
  }
  $join = drupal_container()
    ->get('plugin.manager.views.join')
    ->createInstance('standard', $def);

  // use a short alias for this:
  $alias = $def['table'] . '_' . $this->table;
  $this->alias = $this->query
    ->add_relationship($alias, $join, 'taxonomy_term_data', $this->relationship);
}