3 namespace Drupal\views\Plugin\views\filter;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Database\Database;
9 * Filter handler which allows to search on multiple fields.
11 * @ingroup views_field_handlers
13 * @ViewsFilter("combine")
15 class Combine extends StringFilter {
18 * @var views_plugin_query_default
22 protected function defineOptions() {
23 $options = parent::defineOptions();
24 $options['fields'] = ['default' => []];
29 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
30 parent::buildOptionsForm($form, $form_state);
31 $this->view->initStyle();
33 // Allow to choose all fields as possible
34 if ($this->view->style_plugin->usesFields()) {
36 foreach ($this->view->display_handler->getHandlers('field') as $name => $field) {
37 // Only allow clickSortable fields. Fields without clickSorting will
38 // probably break in the Combine filter.
39 if ($field->clickSortable()) {
40 $options[$name] = $field->adminLabel(TRUE);
46 '#title' => $this->t('Choose fields to combine for filtering'),
47 '#description' => $this->t("This filter doesn't work for very special field handlers."),
49 '#options' => $options,
50 '#default_value' => $this->options['fields'],
54 $form_state->setErrorByName('', $this->t('You have to add some fields to be able to use this filter.'));
59 public function query() {
60 $this->view->_build('field');
62 // Only add the fields if they have a proper field and table alias.
63 foreach ($this->options['fields'] as $id) {
64 // Overridden fields can lead to fields missing from a display that are
65 // still set in the non-overridden combined filter.
66 if (!isset($this->view->field[$id])) {
67 // If fields are no longer available that are needed to filter by, make
68 // sure no results are shown to prevent displaying more then intended.
69 $this->view->build_info['fail'] = TRUE;
72 $field = $this->view->field[$id];
73 // Always add the table of the selected fields to be sure a table alias exists.
74 $field->ensureMyTable();
75 if (!empty($field->field_alias) && !empty($field->field_alias)) {
76 $fields[] = "$field->tableAlias.$field->realField";
80 $count = count($fields);
81 $separated_fields = [];
82 foreach ($fields as $key => $field) {
83 $separated_fields[] = $field;
84 if ($key < $count - 1) {
85 $separated_fields[] = "' '";
88 $expression = implode(', ', $separated_fields);
89 $expression = "CONCAT_WS(' ', $expression)";
91 $info = $this->operators();
92 if (!empty($info[$this->operator]['method'])) {
93 $this->{$info[$this->operator]['method']}($expression);
101 public function validate() {
102 $errors = parent::validate();
103 if ($this->displayHandler->usesFields()) {
104 $fields = $this->displayHandler->getHandlers('field');
105 foreach ($this->options['fields'] as $id) {
106 if (!isset($fields[$id])) {
107 // Combined field filter only works with fields that are in the field
109 $errors[] = $this->t('Field %field set in %filter is not set in display %display.', ['%field' => $id, '%filter' => $this->adminLabel(), '%display' => $this->displayHandler->display['display_title']]);
112 elseif (!$fields[$id]->clickSortable()) {
113 // Combined field filter only works with simple fields. If the field
114 // is not click sortable we can assume it is not a simple field.
115 // @todo change this check to isComputed. See
116 // https://www.drupal.org/node/2349465
117 $errors[] = $this->t('Field %field set in %filter is not usable for this filter type. Combined field filter only works for simple fields.', ['%field' => $fields[$id]->adminLabel(), '%filter' => $this->adminLabel()]);
122 $errors[] = $this->t('%display: %filter can only be used on displays that use fields. Set the style or row format for that display to one using fields to use the combine field filter.', ['%display' => $this->displayHandler->display['display_title'], '%filter' => $this->adminLabel()]);
128 * By default things like opEqual uses add_where, that doesn't support
129 * complex expressions, so override opEqual (and all operators below).
131 public function opEqual($expression) {
132 $placeholder = $this->placeholder();
133 $operator = $this->operator();
134 $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->value]);
137 protected function opContains($expression) {
138 $placeholder = $this->placeholder();
139 $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . db_like($this->value) . '%']);
143 * Filters by one or more words.
145 * By default opContainsWord uses add_where, that doesn't support complex
148 * @param string $expression
150 protected function opContainsWord($expression) {
151 $placeholder = $this->placeholder();
153 // Don't filter on empty strings.
154 if (empty($this->value)) {
158 // Match all words separated by spaces or sentences encapsulated by double
160 preg_match_all(static::WORDS_PATTERN, ' ' . $this->value, $matches, PREG_SET_ORDER);
162 // Switch between the 'word' and 'allwords' operator.
163 $type = $this->operator == 'word' ? 'OR' : 'AND';
164 $group = $this->query->setWhereGroup($type);
165 $operator = Database::getConnection()->mapConditionOperator('LIKE');
166 $operator = isset($operator['operator']) ? $operator['operator'] : 'LIKE';
168 foreach ($matches as $match_key => $match) {
169 $temp_placeholder = $placeholder . '_' . $match_key;
170 // Clean up the user input and remove the sentence delimiters.
171 $word = trim($match[2], ',?!();:-"');
172 $this->query->addWhereExpression($group, "$expression $operator $temp_placeholder", [$temp_placeholder => '%' . Database::getConnection()->escapeLike($word) . '%']);
176 protected function opStartsWith($expression) {
177 $placeholder = $this->placeholder();
178 $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => db_like($this->value) . '%']);
181 protected function opNotStartsWith($expression) {
182 $placeholder = $this->placeholder();
183 $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => db_like($this->value) . '%']);
186 protected function opEndsWith($expression) {
187 $placeholder = $this->placeholder();
188 $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . db_like($this->value)]);
191 protected function opNotEndsWith($expression) {
192 $placeholder = $this->placeholder();
193 $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . db_like($this->value)]);
196 protected function opNotLike($expression) {
197 $placeholder = $this->placeholder();
198 $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . db_like($this->value) . '%']);
201 protected function opRegex($expression) {
202 $placeholder = $this->placeholder();
203 $this->query->addWhereExpression($this->options['group'], "$expression REGEXP $placeholder", [$placeholder => $this->value]);
206 protected function opEmpty($expression) {
207 if ($this->operator == 'empty') {
208 $operator = "IS NULL";
211 $operator = "IS NOT NULL";
214 $this->query->addWhereExpression($this->options['group'], "$expression $operator");