function Sql::buildCondition

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

Construct the "WHERE" or "HAVING" part of the query.

As views has to wrap the conditions from arguments with AND, a special group is wrapped around all conditions. This special group has the ID 0. There is other code in filters which makes sure that the group IDs are higher than zero.

Parameters

$where: 'where' or 'having'.

1 call to Sql::buildCondition()
Sql::query in core/modules/views/src/Plugin/views/query/Sql.php
Generates a query and count query from all of the information supplied.

File

core/modules/views/src/Plugin/views/query/Sql.php, line 1134

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

protected function buildCondition($where = 'where') {
    $has_condition = FALSE;
    $has_arguments = FALSE;
    $has_filter = FALSE;
    
    /** @var \Drupal\Core\Database\Connection $connection */
    $connection = $this->getConnection();
    $main_group = $connection->condition('AND');
    $filter_group = $this->groupOperator == 'OR' ? $connection->condition('OR') : $connection->condition('AND');
    foreach ($this->{$where} as $group => $info) {
        if (!empty($info['conditions'])) {
            $sub_group = $info['type'] == 'OR' ? $connection->condition('OR') : $connection->condition('AND');
            foreach ($info['conditions'] as $clause) {
                if ($clause['operator'] == 'formula') {
                    $has_condition = TRUE;
                    $sub_group->where($clause['field'], $clause['value']);
                }
                else {
                    $has_condition = TRUE;
                    $sub_group->condition($clause['field'], $clause['value'], $clause['operator']);
                }
            }
            // Add the item to the filter group.
            if ($group != 0) {
                $has_filter = TRUE;
                $filter_group->condition($sub_group);
            }
            else {
                $has_arguments = TRUE;
                $main_group->condition($sub_group);
            }
        }
    }
    if ($has_filter) {
        $main_group->condition($filter_group);
    }
    if (!$has_arguments && $has_condition) {
        return $filter_group;
    }
    if ($has_arguments && $has_condition) {
        return $main_group;
    }
}

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