You are here

public function JoinPluginBase::buildJoin in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/join/JoinPluginBase.php \Drupal\views\Plugin\views\join\JoinPluginBase::buildJoin()

Builds the SQL for the join this object represents.

When possible, try to use table alias instead of table names.

Parameters

$select_query: An select query object.

$table: The base table to join.

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

Overrides JoinPluginInterface::buildJoin

1 call to JoinPluginBase::buildJoin()
JoinTest::buildJoin in core/modules/views/tests/modules/views_test_data/src/Plugin/views/join/JoinTest.php
Builds the SQL for the join this object represents.
2 methods override JoinPluginBase::buildJoin()
JoinTest::buildJoin in core/modules/views/tests/modules/views_test_data/src/Plugin/views/join/JoinTest.php
Builds the SQL for the join this object represents.
Subquery::buildJoin in core/modules/views/src/Plugin/views/join/Subquery.php
Builds the SQL for the join this object represents.

File

core/modules/views/src/Plugin/views/join/JoinPluginBase.php, line 248
Contains \Drupal\views\Plugin\views\join\JoinPluginBase.

Class

JoinPluginBase
Represents a join and creates the SQL necessary to implement the join.

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'];
  }
  if ($this->leftTable) {
    $left = $view_query
      ->getTableInfo($this->leftTable);
    $left_field = "{$left['alias']}.{$this->leftField}";
  }
  else {

    // This can be used if left_field is a formula or something. It should be used only *very* rarely.
    $left_field = $this->leftField;
  }
  $condition = "{$left_field} = {$table['alias']}.{$this->field}";
  $arguments = array();

  // Tack on the extra.
  if (isset($this->extra)) {
    if (is_array($this->extra)) {
      $extras = array();
      foreach ($this->extra as $info) {

        // Do not require 'value' to be set; allow for field syntax instead.
        $info += array(
          'value' => NULL,
        );

        // 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'])) {

          // If we're aware of a table alias for this table, use the table
          // alias instead of the table name.
          if (isset($left) && $left['table'] == $info['table']) {
            $join_table = $left['alias'] . '.';
          }
          else {
            $join_table = $info['table'] . '.';
          }
        }

        // Convert a single-valued array of values to the single-value case,
        // and transform from IN() notation to = notation
        if (is_array($info['value']) && count($info['value']) == 1) {
          $info['value'] = array_shift($info['value']);
        }
        if (is_array($info['value'])) {

          // With an array of values, we need multiple placeholders and the
          // 'IN' operator is implicit.
          $local_arguments = array();
          foreach ($info['value'] as $value) {
            $placeholder_i = ':views_join_condition_' . $select_query
              ->nextPlaceholder();
            $local_arguments[$placeholder_i] = $value;
          }
          $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
          $placeholder = '( ' . implode(', ', array_keys($local_arguments)) . ' )';
          $arguments += $local_arguments;
        }
        else {

          // With a single value, the '=' operator is implicit.
          $operator = !empty($info['operator']) ? $info['operator'] : '=';
          $placeholder = ':views_join_condition_' . $select_query
            ->nextPlaceholder();
        }

        // Set 'field' as join table field if available or set 'left field' as
        // join table field is not set.
        if (isset($info['field'])) {
          $join_table_field = "{$join_table}{$info['field']}";

          // Allow the value to be set either with the 'value' element or
          // with 'left_field'.
          if (isset($info['left_field'])) {
            $placeholder = "{$left['alias']}.{$info['left_field']}";
          }
          else {
            $arguments[$placeholder] = $info['value'];
          }
        }
        else {
          $join_table_field = "{$left['alias']}.{$info['left_field']}";
          $arguments[$placeholder] = $info['value'];
        }
        $extras[] = "{$join_table_field} {$operator} {$placeholder}";
      }
      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);
}