From 7e1dcd656169b01adf3109c3b72f7cc3b163bb8f Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Wed, 24 May 2023 08:46:49 +1000
Subject: [PATCH] Issue #2823910 by daffie, pwolanin, smustgrave, neclimdul,
 larowlan, dawehner: DBTNG/EQ condition works inconsistently with arrays

(cherry picked from commit 485604fe3a3847bfe8f11c4e78a92bce839aef2c)
---
 core/lib/Drupal/Core/Database/Query/Condition.php  | 10 ++++++++--
 .../KernelTests/Core/Database/SelectTest.php       | 14 ++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/core/lib/Drupal/Core/Database/Query/Condition.php b/core/lib/Drupal/Core/Database/Query/Condition.php
index 5cfd1a1100d7..616255db2f76 100644
--- a/core/lib/Drupal/Core/Database/Query/Condition.php
+++ b/core/lib/Drupal/Core/Database/Query/Condition.php
@@ -106,8 +106,14 @@ public function condition($field, $value = NULL, $operator = '=') {
       throw new InvalidQueryException(sprintf("Query condition '%s %s ()' cannot be empty.", $field, $operator));
     }
     if (is_array($value) && in_array($operator, ['=', '<', '>', '<=', '>=', 'IS NULL', 'IS NOT NULL'], TRUE)) {
-      $value = implode(', ', $value);
-      throw new InvalidQueryException(sprintf("Query condition '%s %s %s' must have an array compatible operator.", $field, $operator, $value));
+      if (count($value) > 1) {
+        $value = implode(', ', $value);
+        throw new InvalidQueryException(sprintf("Query condition '%s %s %s' must have an array compatible operator.", $field, $operator, $value));
+      }
+      else {
+        $value = $value[0];
+        @trigger_error('Calling ' . __METHOD__ . '() without an array compatible operator is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3350985', E_USER_DEPRECATED);
+      }
     }
 
     $this->conditions[] = [
diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
index 798b4dc5640c..777c8e9b8220 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
@@ -627,4 +627,18 @@ public function testNonArrayOperatorWithArrayValueCondition($operator, $operator
       ->execute();
   }
 
+  /**
+   * Tests thrown exception for non array operator conditions with array value.
+   *
+   * @dataProvider providerNonArrayOperatorWithArrayValueCondition
+   * @group legacy
+   */
+  public function testNonArrayOperatorWithArrayValueConditionDeprecated($operator, $operator_in_exception_message) {
+    $this->expectDeprecation('Calling Drupal\Core\Database\Query\Condition::condition() without an array compatible operator is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3350985');
+    $this->connection->select('test', 't')
+      ->fields('t')
+      ->condition('age', [26], $operator)
+      ->execute();
+  }
+
 }
-- 
GitLab