Skip to content
Snippets Groups Projects
Commit cda222ec authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2808321 by vaplas, Jo Fitzgerald, Lendude, alexpott, xjm, Chi,...

Issue #2808321 by vaplas, Jo Fitzgerald, Lendude, alexpott, xjm, Chi, benqwerty: Fix default value "EQUAL" in $query_operator param of BooleanOperator::queryOpBoolean()
parent 8cc93d06
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -74,14 +74,14 @@ protected function operators() {
'method' => 'queryOpBoolean',
'short' => $this->t('='),
'values' => 1,
'query_operator' => static::EQUAL,
'query_operator' => self::EQUAL,
],
'!=' => [
'title' => $this->t('Is not equal to'),
'method' => 'queryOpBoolean',
'short' => $this->t('!='),
'values' => 1,
'query_operator' => static::NOT_EQUAL,
'query_operator' => self::NOT_EQUAL,
],
];
}
......@@ -231,13 +231,13 @@ public function query() {
* @param string $field
* The field name to add the where condition for.
* @param string $query_operator
* (optional) Either static::EQUAL or static::NOT_EQUAL. Defaults to
* static::EQUAL.
* (optional) Either self::EQUAL or self::NOT_EQUAL. Defaults to
* self::EQUAL.
*/
protected function queryOpBoolean($field, $query_operator = EQUAL) {
protected function queryOpBoolean($field, $query_operator = self::EQUAL) {
if (empty($this->value)) {
if ($this->accept_null) {
if ($query_operator == static::EQUAL) {
if ($query_operator === self::EQUAL) {
$condition = (new Condition('OR'))
->condition($field, 0, $query_operator)
->isNull($field);
......@@ -255,12 +255,13 @@ protected function queryOpBoolean($field, $query_operator = EQUAL) {
}
else {
if (!empty($this->definition['use_equal'])) {
// Forces an '=' operator instead of a '<>' for performance reasons.
if ($query_operator == static::EQUAL) {
$this->query->addWhere($this->options['group'], $field, 1, static::EQUAL);
// Forces a self::EQUAL operator instead of a self::NOT_EQUAL for
// performance reasons.
if ($query_operator === self::EQUAL) {
$this->query->addWhere($this->options['group'], $field, 1, self::EQUAL);
}
else {
$this->query->addWhere($this->options['group'], $field, 0, static::EQUAL);
$this->query->addWhere($this->options['group'], $field, 0, self::EQUAL);
}
}
else {
......
<?php
namespace Drupal\views_test_data\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\BooleanOperator;
/**
* Filter to test queryOpBoolean() with default operator.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("boolean_default")
*/
class FilterBooleanOperatorDefaultTest extends BooleanOperator {
/**
* {@inheritdoc}
*/
public function query() {
$this->ensureMyTable();
$field = "$this->tableAlias.$this->realField";
$this->queryOpBoolean($field);
}
}
<?php
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
/**
* Tests the queryOpBoolean() with default operator.
*
* @group views
* @see \Drupal\views\Plugin\views\filter\BooleanOperator
*/
class FilterBooleanOperatorDefaultTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'views_test_data'];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_view'];
/**
* {@inheritdoc}
*/
protected function viewsData() {
$views_data = parent::viewsData();
$views_data['views_test_data']['status']['filter']['id'] = 'boolean_default';
return $views_data;
}
/**
* Tests the queryOpBoolean() with default operator.
*/
public function testFilterBooleanOperatorDefault() {
$view = Views::getView('test_view');
$view->setDisplay();
$view->displayHandlers->get('default')->overrideOption('filters', [
'status' => [
'id' => 'status',
'field' => 'status',
'table' => 'views_test_data',
'value' => 0,
],
]);
$this->executeView($view);
$this->assertCount(2, $view->result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment