Skip to content

Instantly share code, notes, and snippets.

@orakili
Created June 29, 2012 07:52
Show Gist options
  • Save orakili/3016549 to your computer and use it in GitHub Desktop.
Save orakili/3016549 to your computer and use it in GitHub Desktop.
Drupal 7 - Search API views - "is empty" and "is not empty" operators.
diff --git a/contrib/search_api_views/includes/handler_filter.inc b/contrib/search_api_views/includes/handler_filter.inc
index cc965da..3ac19f0 100644
--- a/contrib/search_api_views/includes/handler_filter.inc
+++ b/contrib/search_api_views/includes/handler_filter.inc
@@ -37,6 +37,8 @@ class SearchApiViewsHandlerFilter extends views_handler_filter {
'<>' => t('Is not equal to'),
'>=' => t('Is greater than or equal to'),
'>' => t('Is greater than'),
+ 'NULL' => t('Is empty'),
+ 'NOT NULL' => t('Is not empty'),
);
}
@@ -59,11 +61,19 @@ class SearchApiViewsHandlerFilter extends views_handler_filter {
* Add this filter to the query.
*/
public function query() {
- while (is_array($this->value)) {
- $this->value = $this->value ? reset($this->value) : NULL;
+ if ($this->operator === 'NULL') {
+ $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
+ }
+ elseif ($this->operator === 'NOT NULL') {
+ $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
}
- if ($this->value) {
- $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
+ else {
+ while (is_array($this->value)) {
+ $this->value = $this->value ? reset($this->value) : NULL;
+ }
+ if ($this->value) {
+ $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment