View source
<?php
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
class BooleanOperator extends FilterPluginBase {
const EQUAL = '=';
const NOT_EQUAL = '<>';
protected $alwaysMultiple = TRUE;
public $accept_null = FALSE;
public function operatorOptions($which = 'title') {
$options = [];
foreach ($this
->operators() as $id => $info) {
$options[$id] = $info[$which];
}
return $options;
}
protected function operators() {
return [
'=' => [
'title' => $this
->t('Is equal to'),
'method' => 'queryOpBoolean',
'short' => $this
->t('='),
'values' => 1,
'query_operator' => self::EQUAL,
],
'!=' => [
'title' => $this
->t('Is not equal to'),
'method' => 'queryOpBoolean',
'short' => $this
->t('!='),
'values' => 1,
'query_operator' => self::NOT_EQUAL,
],
];
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->value_value = $this
->t('True');
if (isset($this->definition['label'])) {
$this->value_value = $this->definition['label'];
}
elseif (isset($this->definition['title'])) {
$this->value_value = $this->definition['title'];
}
if (isset($this->definition['accept null'])) {
$this->accept_null = (bool) $this->definition['accept null'];
}
elseif (isset($this->definition['accept_null'])) {
$this->accept_null = (bool) $this->definition['accept_null'];
}
$this->valueOptions = NULL;
}
public function getValueOptions() {
if (isset($this->definition['type'])) {
if ($this->definition['type'] == 'yes-no') {
$this->valueOptions = [
1 => $this
->t('Yes'),
0 => $this
->t('No'),
];
}
if ($this->definition['type'] == 'on-off') {
$this->valueOptions = [
1 => $this
->t('On'),
0 => $this
->t('Off'),
];
}
if ($this->definition['type'] == 'enabled-disabled') {
$this->valueOptions = [
1 => $this
->t('Enabled'),
0 => $this
->t('Disabled'),
];
}
}
if (!isset($this->valueOptions)) {
$this->valueOptions = [
1 => $this
->t('True'),
0 => $this
->t('False'),
];
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['value']['default'] = FALSE;
return $options;
}
protected function valueForm(&$form, FormStateInterface $form_state) {
if (empty($this->valueOptions)) {
$this
->getValueOptions();
}
if ($exposed = $form_state
->get('exposed')) {
$filter_form_type = 'select';
}
else {
$filter_form_type = 'radios';
}
$form['value'] = [
'#type' => $filter_form_type,
'#title' => $this->value_value,
'#options' => $this->valueOptions,
'#default_value' => $this->value,
];
if (!empty($this->options['exposed'])) {
$identifier = $this->options['expose']['identifier'];
$user_input = $form_state
->getUserInput();
if ($exposed && !isset($user_input[$identifier])) {
$user_input[$identifier] = $this->value;
$form_state
->setUserInput($user_input);
}
if (!$exposed || empty($this->options['expose']['required'])) {
$form['value']['#options'] = [
'All' => $this
->t('- Any -'),
] + $form['value']['#options'];
}
}
}
protected function valueValidate($form, FormStateInterface $form_state) {
if ($form_state
->getValue([
'options',
'value',
]) == 'All' && !$form_state
->isValueEmpty([
'options',
'expose',
'required',
])) {
$form_state
->setErrorByName('value', $this
->t('You must select a value unless this is an non-required exposed filter.'));
}
}
public function adminSummary() {
if ($this
->isAGroup()) {
return $this
->t('grouped');
}
if (!empty($this->options['exposed'])) {
return $this
->t('exposed');
}
if (empty($this->valueOptions)) {
$this
->getValueOptions();
}
return $this->operator . ' ' . $this->valueOptions[!empty($this->value)];
}
public function defaultExposeOptions() {
parent::defaultExposeOptions();
$this->options['expose']['operator_id'] = '';
$this->options['expose']['label'] = $this->value_value;
$this->options['expose']['required'] = TRUE;
}
public function query() {
$this
->ensureMyTable();
$field = "{$this->tableAlias}.{$this->realField}";
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
call_user_func([
$this,
$info[$this->operator]['method'],
], $field, $info[$this->operator]['query_operator']);
}
}
protected function queryOpBoolean($field, $query_operator = self::EQUAL) {
if (empty($this->value)) {
if ($this->accept_null) {
if ($query_operator === self::EQUAL) {
$condition = $this->query
->getConnection()
->condition('OR')
->condition($field, 0, $query_operator)
->isNull($field);
}
else {
$condition = $this->query
->getConnection()
->condition('AND')
->condition($field, 0, $query_operator)
->isNotNull($field);
}
$this->query
->addWhere($this->options['group'], $condition);
}
else {
$this->query
->addWhere($this->options['group'], $field, 0, $query_operator);
}
}
else {
if (!empty($this->definition['use_equal'])) {
if ($query_operator === self::EQUAL) {
$this->query
->addWhere($this->options['group'], $field, 1, self::EQUAL);
}
else {
$this->query
->addWhere($this->options['group'], $field, 0, self::EQUAL);
}
}
else {
$this->query
->addWhere($this->options['group'], $field, 1, $query_operator);
}
}
}
}