You are here

public function Combine::query in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::query()
  2. 9 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::query()

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides StringFilter::query

File

core/modules/views/src/Plugin/views/filter/Combine.php, line 58

Class

Combine
Filter handler which allows to search on multiple fields.

Namespace

Drupal\views\Plugin\views\filter

Code

public function query() {
  $this->view
    ->_build('field');
  $fields = [];

  // Only add the fields if they have a proper field and table alias.
  foreach ($this->options['fields'] as $id) {

    // Overridden fields can lead to fields missing from a display that are
    // still set in the non-overridden combined filter.
    if (!isset($this->view->field[$id])) {

      // If fields are no longer available that are needed to filter by, make
      // sure no results are shown to prevent displaying more then intended.
      $this->view->build_info['fail'] = TRUE;
      continue;
    }
    $field = $this->view->field[$id];

    // Always add the table of the selected fields to be sure a table alias exists.
    $field
      ->ensureMyTable();
    if (!empty($field->field_alias) && !empty($field->field_alias)) {
      $fields[] = "{$field->tableAlias}.{$field->realField}";
    }
  }
  if ($fields) {

    // We do not use the CONCAT_WS operator when there is only a single field.
    // Using the CONCAT_WS operator with a single field is not a problem for
    // the by core supported databases. Only the MS SQL Server requires the
    // CONCAT_WS operator to be used with at least three arguments.
    if (count($fields) == 1) {
      $expression = reset($fields);
    }
    else {

      // Multiple fields are separated by 3 spaces so that so that search
      // strings that contain spaces are still only matched to single field
      // values and not to multi-field values that exist only because we do
      // the concatenation/LIKE trick.
      $expression = implode(", ' ', ", $fields);
      $expression = "CONCAT_WS(' ', {$expression})";
    }
    $info = $this
      ->operators();
    if (!empty($info[$this->operator]['method'])) {
      $this
        ->{$info[$this->operator]['method']}($expression);
    }
  }
}