Drupal: How to restrict apachesolr search results by user/article facets

665 Views Asked by At

I have a wiki built with drupal, with a taxonomy category Workgroup, assigned to both the users and the articles. I am using apache solr search module with facet api and my end goal is to set up the search so that by default when users search for the articles, only articles from their workgroup are shown.

That is, when they launch the search from a search box, they should get the same results as for /search/site/hello?f[0]=im_field_kb_workgroups%3A4529 (where 4529 is one workgroup id) instead of just /search/site/hello (current behavior) Users should still be allowed to search in other workgroup facets when they want, by removing the checkbox in the facet block.

I have this working almost by hacking the apachesolr module (not recommended I know but really want this to work). In the function apachesolr_search_custom_page_search_form_submit, I have:

  // Get the workgroup id
  global $user;
  $account = user_load($user->uid);
  $user_kb_wg_fieldinfo = field_get_items('user', $account, 'field_kb_workgroups');
  $user_kb_wg_tid= '';
  if ($user_kb_wg_fieldinfo) {
    $user_kb_wg_tid = $user_kb_wg_fieldinfo[0]['tid'];
  }

  // Add the solr filter for this workgroup facet so that by default, search results are
  // fetched only from that workgroup.
  if ($user_kb_wg_tid === '4529') {
    $get['f[0]'] = 'im_field_kb_workgroups:4529';
  }

This does the job but the problem is that this relies on the apachesolr search form. I have users coming to the wiki by searching from sites external to the wiki, where the wiki search form is just a simple POST form pointing to the KB domain and the path /search. So this will work only when people are searching from inside the wiki, where I present them the apachesolr search form.

I have investigated some other options:

In my custom module, I implement this hook (without the user workgroup checks for now, for testing):

function kb_hacks_apachesolr_query_prepare($query) {
  $query->addFilter('im_field_kb_workgroups', '4529');
}

This filters the results from searches everywhere, but the filter is applied all the time, and users don't get to deselect this or other filters. (in fact, other filters appear only when passing the filter as a GET param like above with f[0])

I also played with the url_inbound_alter hook, but could not figure out how to pass the solr query param as GET. The following did not work.

function kb_hacks_url_inbound_alter(&$path, $original_path, $path_language) {
  if ($path == 'search/site/hello') {
    $_GET['f[0]'] = "im_field_kb_workgroups:4529";
    //$_GET['f[0]'] = "im_field_kb_workgroups%3A4529";
    //$path = 'search/site/hello?f[0]=im_field_kb_workgroups%3A4529;
  }
}

Is there a way to set GET params from this hook? But even if this had worked, I would still have to figure out how to do this only by default (when the search form is submitted), and not when the filter itself is deselected. Is there a way to detect checkbox changes in the facet block?

Maybe there's another way to do this? I have been stuck here for the last two days and would really appreciate any help I can get. Thanks!

1

There are 1 best solutions below

1
On

You can add a relationship to the taxonomy term "Workgroup" and use a contextual filter for the current user. In the contextual filters section, you can change the behavior when the filter is not present.