I have a user filter (autocomplete field) on a search_api view. Running the view with a user name entered produces the expected results. However running the view with no user name in this field filters by UID = 0, producing no results. The "required" checkbox is not checked.

I suspect that this is an issue for any autcomplete entity reference filter field, but I have not tested.

I was able to get my view to work with the following, but this is not a good solution (on at least a couple levels):

MYMODULE_search_api_views_query_alter($view) {
  // Search_api_views treats an empty user filter field as filtering by a UID of zero
  // rather than ignoring it
  $fix_fields = array();
  foreach($view->filter as $field => $handler) {
    if(get_class($handler) == 'SearchApiViewsHandlerFilterUser') {
      $fix_fields[] = $field;
    }
  }
  if(count($fix_fields)) {
    foreach($view->query->where as $wk => $where) {
      foreach($where['conditions'] as $ck => $condition) {
        if(in_array($condition[0], $fix_fields) && $condition[1] == 0) {
          unset($view->query->where[$wk]['conditions'][$ck]);
        }
      }
    }
  }
}

Comments

pdcarto’s picture

The attached patch against 7.x-1.x-dev fixed the problem for me, though I seriously doubt that this is the right approach...

drunken monkey’s picture

Status: Active » Closed (duplicate)

See #2198791: Indexed Content: Author as Exposed Filter: empty field treated as zero (Anonymous). Please help getting this fixed by testing and providing feedback there!