From 890aa81439ee93235ceeeca21bccc0a425917269 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Claes=20Gyllensv=C3=A4rd?= <letharion@gmail.com>
Date: Tue, 7 Aug 2012 14:20:09 +0200
Subject: [PATCH] Move query logic from hook_views_query_alter into the filter
 handler

---
 handlers/vms_handler_filter_mlid.inc |   17 +++++++++++++++++
 vms.views.inc                        |   22 ----------------------
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/handlers/vms_handler_filter_mlid.inc b/handlers/vms_handler_filter_mlid.inc
index c394594..bff3147 100644
--- a/handlers/vms_handler_filter_mlid.inc
+++ b/handlers/vms_handler_filter_mlid.inc
@@ -46,4 +46,21 @@ class vms_handler_filter_mlid extends views_handler_filter_equality {
   function can_expose() {
     return FALSE;
   }
+
+  /**
+   * Add the filtering to the query.
+   *
+   * Potentially redundant if the parent class could be replaced with an IN
+   * handler.
+   */
+  public function query() {
+    $this->ensure_my_table();
+
+    $this->query->add_where(
+      $this->options['group'],
+      "$this->table_alias.$this->real_field",
+      vms_get_current_mlid($this->options['value']),
+      $this->operator = ($this->operator == '=') ? 'IN' : 'NOT IN'
+    );
+  }
 }
diff --git a/vms.views.inc b/vms.views.inc
index 29b674e..0bcaa76 100755
--- a/vms.views.inc
+++ b/vms.views.inc
@@ -44,25 +44,3 @@ function vms_views_data_alter(&$data) {
     }
   }
 }
-
-/**
- * Implements hook_views_query_alter().
- *
- * This function is used instead of hook_views_query_substitutions, since it
- * has to change not only filter values, but also operators.
- */
-function vms_views_query_alter($view, $query) {
-  // Get the patterns we want to replace.
-  $replacements = array_keys(vms_mlid_query_types());
-
-  if (isset($view->filter['vms_field_promo_mlid_direct']) || isset($view->filter['vms_field_promo_mlid_sub'])) {
-    foreach ($query->where as &$condition_group) {
-      foreach ($condition_group['conditions'] as &$condition) {
-        if (in_array($condition['value'], $replacements, TRUE)) {
-          $condition['operator'] = ($condition['operator'] == '=') ? 'IN' : 'NOT IN';
-          $condition['value'] = vms_get_current_mlid($condition['value']);
-        }
-      }
-    }
-  }
-}
-- 
1.7.9.5

