public function IndexNameDepth::query in Views Taxonomy Term Name Depth 7.x
Same name and namespace in other branches
- 8.6 src/Plugin/views/argument/IndexNameDepth.php \Drupal\views_taxonomy_term_name_depth\Plugin\views\argument\IndexNameDepth::query()
- 8 src/Plugin/views/argument/IndexNameDepth.php \Drupal\views_taxonomy_term_name_depth\Plugin\views\argument\IndexNameDepth::query()
- 8.3 src/Plugin/views/argument/IndexNameDepth.php \Drupal\views_taxonomy_term_name_depth\Plugin\views\argument\IndexNameDepth::query()
Set up the query for this argument.
The argument sent may be found at $this->argument.
Overrides ArgumentPluginBase::query
File
- src/
Plugin/ views/ argument/ IndexNameDepth.php, line 159
Class
- IndexNameDepth
- Argument handler for taxonomy terms with depth.
Namespace
Drupal\views_taxonomy_term_name_depth\Plugin\views\argumentCode
public function query($group_by = FALSE) {
$this
->ensureMyTable();
if (!empty($this->options['break_phrase'])) {
$break = static::breakString($this->argument);
if ($break->value === [
-1,
]) {
return FALSE;
}
$operator = count($break->value) > 1 ? 'IN' : '=';
$tids = $break->value;
}
else {
$operator = "=";
$tids = $this->argument;
}
// Now build the subqueries.
if (is_string($tids)) {
if ($this->moduleHandler
->moduleExists('pathauto')) {
$query = $this->database
->select('taxonomy_term_field_data', 't')
->fields('t', [
'tid',
'name',
]);
// Filter by vocabulary ID if one or more are provided.
if (!empty($this->options['vocabularies'])) {
$query
->condition('t.vid', $this->options['vocabularies'], 'IN');
}
$results = $query
->execute()
->fetchAll(\PDO::FETCH_OBJ);
// Iterate results.
foreach ($results as $row) {
if ($this->pathautoAliasCleaner
->cleanString($row->name) == $this->pathautoAliasCleaner
->cleanString($tids)) {
$tids = $row->tid;
}
}
}
else {
// Replaces "-" with space if exist.
$argument = str_replace('-', ' ', $tids);
$query = $this->database
->select('taxonomy_term_field_data', 't')
->fields('t', [
'tid',
'name',
]);
// Filter by vocabulary ID if one or more are provided.
if (!empty($this->options['vocabularies'])) {
$query
->condition('t.vid', $this->options['vocabularies'], 'IN');
}
$query
->condition('t.name', $argument, '=');
$results = $query
->execute()
->fetchAll(\PDO::FETCH_OBJ);
// Iterate results.
foreach ($results as $row) {
$tids = $row->tid;
}
}
}
// Now build the subqueries.
$subquery = $this->database
->select('taxonomy_index', 'tn');
$subquery
->addField('tn', 'nid');
$where = (new Condition('OR'))
->condition('tn.tid', $tids, $operator);
$last = "tn";
if ($this->options['depth'] > 0) {
$subquery
->leftJoin('taxonomy_term__parent', 'tp', "tp.entity_id = tn.tid");
$last = "tp";
foreach (range(1, abs($this->options['depth'])) as $count) {
$subquery
->leftJoin('taxonomy_term__parent', "tp{$count}", "{$last}.parent_target_id = tp{$count}.entity_id");
$where
->condition("tp{$count}.entity_id", $tids, $operator);
$last = "tp{$count}";
}
}
elseif ($this->options['depth'] < 0) {
foreach (range(1, abs($this->options['depth'])) as $count) {
$field = $count == 1 ? 'tid' : 'entity_id';
$subquery
->leftJoin('taxonomy_term__parent', "tp{$count}", "{$last}.{$field} = tp{$count}.parent_target_id");
$where
->condition("tp{$count}.entity_id", $tids, $operator);
$last = "tp{$count}";
}
}
$subquery
->condition($where);
$this->query
->addWhere(0, "{$this->tableAlias}.{$this->realField}", $subquery, 'IN');
}