function Subquery::buildJoin

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/join/Subquery.php \Drupal\views\Plugin\views\join\Subquery::buildJoin()
  2. 8.9.x core/modules/views/src/Plugin/views/join/Subquery.php \Drupal\views\Plugin\views\join\Subquery::buildJoin()
  3. 11.x core/modules/views/src/Plugin/views/join/Subquery.php \Drupal\views\Plugin\views\join\Subquery::buildJoin()

Builds the SQL for the join this object represents.

Parameters

\Drupal\Core\Database\Query\SelectInterface $select_query: The select query object.

string $table: The base table to join.

\Drupal\views\Plugin\views\query\QueryPluginBase $view_query: The source views query.

Overrides JoinPluginBase::buildJoin

File

core/modules/views/src/Plugin/views/join/Subquery.php, line 49

Class

Subquery
Join handler for relationships that join with a subquery as the left field.

Namespace

Drupal\views\Plugin\views\join

Code

public function buildJoin($select_query, $table, $view_query) {
    if (empty($this->configuration['table formula'])) {
        $right_table = "{" . $this->table . "}";
    }
    else {
        $right_table = $this->configuration['table formula'];
    }
    // Add our join condition, using a subquery on the left instead of a field.
    $condition = "({$this->left_query}) = {$table['alias']}.{$this->field}";
    $arguments = [];
    // Tack on the extra.
    // This is just copied verbatim from the parent class, which itself has a
    // bug: https://www.drupal.org/node/1118100.
    if (isset($this->extra)) {
        if (is_array($this->extra)) {
            $extras = [];
            foreach ($this->extra as $info) {
                // Figure out the table name. Remember, only use aliases provided
                // if at all possible.
                $join_table = '';
                if (!array_key_exists('table', $info)) {
                    $join_table = $table['alias'] . '.';
                }
                elseif (isset($info['table'])) {
                    $join_table = $info['table'] . '.';
                }
                $placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
                if (is_array($info['value'])) {
                    $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
                    // Transform from IN() notation to = notation if just one value.
                    if (count($info['value']) == 1) {
                        $info['value'] = array_shift($info['value']);
                        $operator = $operator == 'NOT IN' ? '!=' : '=';
                    }
                }
                else {
                    $operator = !empty($info['operator']) ? $info['operator'] : '=';
                }
                $extras[] = "{$join_table}{$info['field']} {$operator} {$placeholder}";
                $arguments[$placeholder] = $info['value'];
            }
            if ($extras) {
                if (count($extras) == 1) {
                    $condition .= ' AND ' . array_shift($extras);
                }
                else {
                    $condition .= ' AND (' . implode(' ' . $this->extraOperator . ' ', $extras) . ')';
                }
            }
        }
        elseif ($this->extra && is_string($this->extra)) {
            $condition .= " AND ({$this->extra})";
        }
    }
    $select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.