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?

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;
    }
  }
}
| improve this answer | |
  • 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. – Patrick Kenny Jun 9 '15 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! – kyletaylored Jun 12 '15 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; – Patrick Kenny Jun 14 '15 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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