View source
<?php
namespace Drupal\config_views\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\StringFilter;
class StringEntity extends StringFilter {
public $query;
public function operators() {
$operators = [
'=' => [
'title' => $this
->t('Is equal to'),
'short' => $this
->t('='),
'method' => 'opSimple',
'values' => 1,
],
'<>' => [
'title' => $this
->t('Is not equal to'),
'short' => $this
->t('<>'),
'method' => 'opSimple',
'values' => 1,
],
'CONTAINS' => [
'title' => $this
->t('Contains'),
'short' => $this
->t('contains'),
'method' => 'opSimple',
'values' => 1,
],
'STARTS_WITH' => [
'title' => $this
->t('Starts with'),
'short' => $this
->t('begins'),
'method' => 'opSimple',
'values' => 1,
],
'ENDS_WITH' => [
'title' => $this
->t('Ends with'),
'short' => $this
->t('ends'),
'method' => 'opSimple',
'values' => 1,
],
];
if (!empty($this->definition['allow empty'])) {
$operators += [
'IS NULL' => [
'title' => $this
->t('Is empty (NULL)'),
'method' => 'opEmpty',
'short' => $this
->t('empty'),
'values' => 0,
],
'IS NOT NULL' => [
'title' => $this
->t('Is not empty (NOT NULL)'),
'method' => 'opNotEmpty',
'short' => $this
->t('not empty'),
'values' => 0,
],
];
}
return $operators;
}
public function query() {
$field = $this->realField;
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($field);
}
}
public function opSimple($field) {
$this->query
->condition($this->options['group'], $field, $this->value, $this->operator);
}
protected function opEmpty($field) {
$this->query
->exists($this->options['group'], $field);
}
protected function opNotEmpty($field) {
$this->query
->notExists($this->options['group'], $field);
}
}