View source
<?php
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
class Combine extends StringFilter {
var $query;
protected function defineOptions() {
$options = parent::defineOptions();
$options['fields'] = array(
'default' => array(),
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$this->view
->initStyle();
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' => $this
->t('Choose fields to combine for filtering'),
'#description' => $this
->t("This filter doesn't work for very special field handlers."),
'#multiple' => TRUE,
'#options' => $options,
'#default_value' => $this->options['fields'],
);
}
else {
$form_state
->setErrorByName('', $this
->t('You have to add some fields to be able to use this filter.'));
}
}
}
public function query() {
$this->view
->_build('field');
$fields = array();
foreach ($this->options['fields'] as $id) {
if (!isset($this->view->field[$id])) {
$this->view->build_info['fail'] = TRUE;
continue;
}
$field = $this->view->field[$id];
$field
->ensureMyTable();
if (!empty($field->field_alias) && !empty($field->field_alias)) {
$fields[] = "{$field->tableAlias}.{$field->realField}";
}
}
if ($fields) {
$count = count($fields);
$separated_fields = array();
foreach ($fields as $key => $field) {
$separated_fields[] = $field;
if ($key < $count - 1) {
$separated_fields[] = "' '";
}
}
$expression = implode(', ', $separated_fields);
$expression = "CONCAT_WS(' ', {$expression})";
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($expression);
}
}
}
public function validate() {
$errors = parent::validate();
$fields = $this->view->display_handler
->getHandlers('field');
foreach ($this->options['fields'] as $id) {
if (!isset($fields[$id])) {
$errors[] = $this
->t('Field %field set in %filter is not set in this display.', array(
'%field' => $id,
'%filter' => $this
->adminLabel(),
));
break;
}
}
return $errors;
}
function opEqual($expression) {
$placeholder = $this
->placeholder();
$operator = $this
->operator();
$this->query
->addWhereExpression($this->options['group'], "{$expression} {$operator} {$placeholder}", array(
$placeholder => $this->value,
));
}
protected function opContains($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value) . '%',
));
}
protected function opStartsWith($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} LIKE {$placeholder}", array(
$placeholder => db_like($this->value) . '%',
));
}
protected function opNotStartsWith($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} NOT LIKE {$placeholder}", array(
$placeholder => db_like($this->value) . '%',
));
}
protected function opEndsWith($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value),
));
}
protected function opNotEndsWith($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} NOT LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value),
));
}
protected function opNotLike($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} NOT LIKE {$placeholder}", array(
$placeholder => '%' . db_like($this->value) . '%',
));
}
protected function opRegex($expression) {
$placeholder = $this
->placeholder();
$this->query
->addWhereExpression($this->options['group'], "{$expression} REGEXP {$placeholder}", array(
$placeholder => $this->value,
));
}
protected function opEmpty($expression) {
if ($this->operator == 'empty') {
$operator = "IS NULL";
}
else {
$operator = "IS NOT NULL";
}
$this->query
->addWhereExpression($this->options['group'], "{$expression} {$operator}");
}
}