Combine.php in Views (for Drupal 7) 8.3
Definition of Drupal\views\Plugin\views\filter\Combine.
Namespace
Drupal\views\Plugin\views\filterFile
lib/Drupal/views/Plugin/views/filter/Combine.phpView source
<?php
/**
* @file
* Definition of Drupal\views\Plugin\views\filter\Combine.
*/
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Annotation\Plugin;
/**
* Filter handler which allows to search on multiple fields.
*
* @ingroup views_field_handlers
*
* @Plugin(
* id = "combine"
* )
*/
class Combine extends String {
/**
* @var views_plugin_query_default
*/
var $query;
protected function defineOptions() {
$options = parent::defineOptions();
$options['fields'] = array(
'default' => array(),
);
return $options;
}
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
$this->view
->initStyle();
// Allow to choose all fields as possible
if ($this->view->style_plugin
->usesFields()) {
$options = array();
foreach ($this->view->display_handler
->getHandlers('field') as $name => $field) {
$options[$name] = $field
->adminLabel(TRUE);
}
if ($options) {
$form['fields'] = array(
'#type' => 'select',
'#title' => t('Choose fields to combine for filtering'),
'#description' => t("This filter doesn't work for very special field handlers."),
'#multiple' => TRUE,
'#options' => $options,
'#default_value' => $this->options['fields'],
);
}
else {
form_set_error('', t('You have to add some fields to be able to use this filter.'));
}
}
}
public function query() {
$this->view
->_build('field');
$fields = array();
// Only add the fields if they have a proper field and table alias.
foreach ($this->options['fields'] as $id) {
$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) {
$count = count($fields);
$seperated_fields = array();
foreach ($fields as $key => $field) {
$seperated_fields[] = $field;
if ($key < $count - 1) {
$seperated_fields[] = "' '";
}
}
$expression = implode(', ', $seperated_fields);
$expression = "CONCAT_WS(' ', {$expression})";
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($expression);
}
}
}
// By default things like op_equal uses add_where, that doesn't support
// complex expressions, so override all operators.
function op_equal($field) {
$placeholder = $this
->placeholder();
$operator = $this
->operator();
$this->query
->add_where_expression($this->options['group'], "{$field} {$operator} {$placeholder}", array(
$placeholder => $this->value,
));
}
function op_contains($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value) . '%',
));
}
function op_starts($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} LIKE {$placeholder}", array(
$placeholder => db_like($this->value) . '%',
));
}
function op_not_starts($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} NOT LIKE {$placeholder}", array(
$placeholder => db_like($this->value) . '%',
));
}
function op_ends($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value),
));
}
function op_not_ends($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} NOT LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value),
));
}
function op_not($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} NOT LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value) . '%',
));
}
function op_regex($field) {
$placeholder = $this
->placeholder();
$this->query
->add_where_expression($this->options['group'], "{$field} RLIKE {$placeholder}", array(
$placeholder => $this->value,
));
}
function op_empty($field) {
if ($this->operator == 'empty') {
$operator = "IS NULL";
}
else {
$operator = "IS NOT NULL";
}
$this->query
->add_where_expression($this->options['group'], "{$field} {$operator}");
}
}