You are here

public function SearchApiQuery::addWhere in Search API 8

Adds a simple condition to the query.

This replicates the interface of Views' default SQL backend to simplify the Views integration of the Search API. If you are writing Search API-specific Views code, you should better use the addConditionGroup() or addCondition() methods.

Parameters

int $group: The condition group to add these to; groups are used to create AND/OR sections. Groups cannot be nested. Use 0 as the default group. If the group does not yet exist it will be created as an AND group.

string|\Drupal\Core\Database\Query\ConditionInterface|\Drupal\search_api\Query\ConditionGroupInterface $field: The ID of the field to check; or a filter object to add to the query; or, for compatibility purposes, a database condition object to transform into a search filter object and add to the query. If a field ID is passed and starts with a period (.), it will be stripped.

mixed $value: (optional) The value the field should have (or be related to by the operator). Or NULL if an object is passed as $field.

string|null $operator: (optional) The operator to use for checking the constraint. The following operators are supported for primitive types: "=", "<>", "<", "<=", ">=", ">". They have the same semantics as the corresponding SQL operators. If $field is a fulltext field, $operator can only be "=" or "<>", which are in this case interpreted as "contains" or "doesn't contain", respectively. If $value is NULL, $operator also can only be "=" or "<>", meaning the field must have no or some value, respectively. To stay compatible with Views, "!=" is supported as an alias for "<>". If an object is passed as $field, $operator should be NULL.

Return value

$this

See also

\Drupal\views\Plugin\views\query\Sql::addWhere()

\Drupal\search_api\Plugin\views\query\SearchApiQuery::filter()

\Drupal\search_api\Plugin\views\query\SearchApiQuery::condition()

File

src/Plugin/views/query/SearchApiQuery.php, line 1091

Class

SearchApiQuery
Defines a Views query class for searching on Search API indexes.

Namespace

Drupal\search_api\Plugin\views\query

Code

public function addWhere($group, $field, $value = NULL, $operator = NULL) {
  if ($this
    ->shouldAbort()) {
    return $this;
  }

  // Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all the
  // default group.
  if (empty($group)) {
    $group = 0;
  }
  if (is_object($field)) {
    if ($field instanceof ConditionInterface) {
      $field = $this
        ->transformDbCondition($field);
    }
    if ($field instanceof ConditionGroupInterface) {
      $this->where[$group]['condition_groups'][] = $field;
    }
    elseif (!$this
      ->shouldAbort()) {

      // We only need to abort  if that wasn't done by transformDbCondition()
      // already.
      $this
        ->abort('Unexpected condition passed to addWhere().');
    }
  }
  else {
    $condition = [
      $this
        ->sanitizeFieldId($field),
      $value,
      $this
        ->sanitizeOperator($operator),
    ];
    $this->where[$group]['conditions'][] = $condition;
  }
  return $this;
}