From 1c773fed04512ee111bd2cf99666d496042ea827 Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Tue, 24 Dec 2024 10:20:00 +0000
Subject: [PATCH] Issue #3476175 by daffie, rajneeshb, smustgrave, dagmar:
 Change the filter in the overview page of the dblog module to a condition
 object

---
 core/modules/dblog/dblog.admin.inc            |  4 +-
 .../dblog/src/Controller/DbLogController.php  | 50 +++++++++++--------
 2 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/core/modules/dblog/dblog.admin.inc b/core/modules/dblog/dblog.admin.inc
index 0b173997d809..3e7c762065e4 100644
--- a/core/modules/dblog/dblog.admin.inc
+++ b/core/modules/dblog/dblog.admin.inc
@@ -28,14 +28,14 @@ function dblog_filters() {
   if (!empty($types)) {
     $filters['type'] = [
       'title' => t('Type'),
-      'where' => "w.type = ?",
+      'field' => 'w.type',
       'options' => $types,
     ];
   }
 
   $filters['severity'] = [
     'title' => t('Severity'),
-    'where' => 'w.severity = ?',
+    'field' => 'w.severity',
     'options' => RfcLogLevel::getLevels(),
   ];
 
diff --git a/core/modules/dblog/src/Controller/DbLogController.php b/core/modules/dblog/src/Controller/DbLogController.php
index 6459793d6398..9eabd19c5bb4 100644
--- a/core/modules/dblog/src/Controller/DbLogController.php
+++ b/core/modules/dblog/src/Controller/DbLogController.php
@@ -10,6 +10,7 @@
 use Drupal\Core\Controller\ControllerBase;
 use Drupal\Core\Database\Connection;
 use Drupal\Core\Database\Query\PagerSelectExtender;
+use Drupal\Core\Database\Query\SelectInterface;
 use Drupal\Core\Database\Query\TableSortExtender;
 use Drupal\Core\Datetime\DateFormatterInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
@@ -104,7 +105,6 @@ public static function getLogLevelClassMap() {
    */
   public function overview(Request $request) {
 
-    $filter = $this->buildFilterQuery($request);
     $rows = [];
 
     $classes = static::getLogLevelClassMap();
@@ -154,9 +154,8 @@ public function overview(Request $request) {
     ]);
     $query->leftJoin('users_field_data', 'ufd', '[w].[uid] = [ufd].[uid]');
 
-    if (!empty($filter['where'])) {
-      $query->where($filter['where'], $filter['args']);
-    }
+    $this->addFilterToQuery($request, $query);
+
     $result = $query
       ->limit(50)
       ->orderByHeader($header)
@@ -306,14 +305,16 @@ public function eventDetails($event_id) {
   /**
    * Builds a query for database log administration filters based on session.
    *
+   * This method retrieves the session-based filters from the request and applies
+   * them to the provided query object. If no filters are present, the query is
+   * left unchanged.
+   *
    * @param \Symfony\Component\HttpFoundation\Request $request
    *   The request.
-   *
-   * @return array|null
-   *   An associative array with keys 'where' and 'args' or NULL if there were
-   *   no filters set.
+   * @param \Drupal\Core\Database\Query\SelectInterface $query
+   *   The database query.
    */
-  protected function buildFilterQuery(Request $request) {
+  protected function addFilterToQuery(Request $request, SelectInterface &$query): void {
     $session_filters = $request->getSession()->get('dblog_overview_filter', []);
     if (empty($session_filters)) {
       return;
@@ -323,24 +324,29 @@ protected function buildFilterQuery(Request $request) {
 
     $filters = dblog_filters();
 
-    // Build query.
-    $where = $args = [];
+    // Build the condition.
+    $condition_and = $query->getConnection()->condition('AND');
+    $condition_and_used = FALSE;
     foreach ($session_filters as $key => $filter) {
-      $filter_where = [];
+      $condition_or = $query->getConnection()->condition('OR');
+      $condition_or_used = FALSE;
       foreach ($filter as $value) {
-        $filter_where[] = $filters[$key]['where'];
-        $args[] = $value;
+        if ($key == 'severity') {
+          $value = (int) $value;
+        }
+        if (in_array($value, array_keys($filters[$key]['options']))) {
+          $condition_or->condition($filters[$key]['field'], $value);
+          $condition_or_used = TRUE;
+        }
       }
-      if (!empty($filter_where)) {
-        $where[] = '(' . implode(' OR ', $filter_where) . ')';
+      if ($condition_or_used) {
+        $condition_and->condition($condition_or);
+        $condition_and_used = TRUE;
       }
     }
-    $where = !empty($where) ? implode(' AND ', $where) : '';
-
-    return [
-      'where' => $where,
-      'args' => $args,
-    ];
+    if ($condition_and_used) {
+      $query->condition($condition_and);
+    }
   }
 
   /**
-- 
GitLab