1

In a normal Views user search, you can simply add a User: Current filter to remove the logged-in user from the search results.

I have a Search API Views search of a Solr index of users, and I want to do the same thing. However, the User:Current filter is not available. How can I achieve the same function?

1 Answer 1

3
+50

You could write a simple module using hook_views_pre_view.

  1. Be sure to index the User ID
  2. Under Filter Criteria, add Indexed User: User ID, and set it to NOT EQUAL TO and use 0 (anonymous user, which will never be in this list).
  3. Using MY_MODULE_views_pre_view with the global user value, replace 0 with the UID of the current user.
function MY_MODULE_views_pre_view(&$view) {
  global $user;
  if ($view->name == 'VIEW_NAME') {
    if ($user->uid) {
      $view->display['default']->handler->options['filters']['uid']['value'] = $user->uid;
    }
  }
}
3
  • Thanks, this looks like the way to go. How exactly do I do #3? I don't see where USERID is passed to the query. I tried a different approach based on the example in search_api_query_alter() (drupal.org/node/2502819), but ran into a wall. Jun 9, 2015 at 14:35
  • @PatrickKenny I updated my answer, I just had to set up Solr locally to test it. You don't actually need the search_api hook, you can just use a normal views hook. Hope this helps! Jun 12, 2015 at 14:48
  • Thanks, that did the trick! Since my view has multiple panes, I had to change the code to use $view->current_display instead of default as follows: $view->display[$view->current_display]->handler->options['filters']['uid']['value'] = $user->uid; Jun 14, 2015 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.