I've looked at apachesolr.api.php, apachesolr.module, apachesolr_search.module, Peter Wolanin's chapter on Solr in The Definitive Guide to Drupal 7, Solr 1.4: Enterprise Search Server, numerous Solr DrupalCon videos, and extensive searches in this issue queue, but I'm stuck. I want to make numerous custom search blocks that each have different filters ("Search within this content type"). I can get to what I want by clicking on my enabled facets -- I just want to have blocks where these are "pre-selected" (and hidden).

I've implemented hook_block_info() and hook_block_view() to create a few custom search boxes doing something along these lines:

function my_module_block_view ($delta = '') {
  switch ($delta) {
    case 'my_module_search':
      $form = drupal_get_form('search_form');
      $block['subject'] = t('Search this tab of this content type');
      $block['content'] = drupal_render($form);
      return $block;
  }
}

I've altered the form doing something like this:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form') {
    $form['#submit'][] = 'my_module_search_form_submit';
    return;
  }
}

That seems to work. But, when I try to do something like this, it fails:

my_module_search_form_submit($form, &$form_state) {
  $form_state['values']['filters'][] = array(
    '#name' => 'bundle',
    '#value' => 'my_value',
  );
}

I got this format from a hook like this:

function my_module_apachesolr_query_prepare($query) {
  dpm($query->getFilters(), 'Filters');
}

It works for me to include $query->addFilter('bundle', 'my_field'); in hook_apachesolr_query_prepare(), but then the filter is always in place. I've also tried making my own search pages with hook_search_info() and hook_search_execute(), but I can't do this for every piece of content. I've also tried this doing something like http://drupal.org/node/963118, but that, too, does not seem to work for me.

I feel like I'm missing something simple here. Any suggestions would be greatly appreciated!

Comments

nick_vh’s picture

Take a look at the retain filters functionality :

    $form['basic']['apachesolr_search']['get'] = array(
      '#type' => 'hidden',
      '#default_value' => json_encode(array_diff_key($_GET, array('q' => 1, 'page' => 1, 'solrsort' => 1, 'retain-filters' => 1))),
    );

As you can see we are sending various information from the url to the submit function of the form

Then in the submit

/**
 * Added form submit function to retain filters.
 *
 * @see apachesolr_search_form_search_form_alter()
 */
function apachesolr_search_form_search_submit($form, &$form_state) {
  $fv = $form_state['values'];
  $get = json_decode($fv['apachesolr_search']['get'], TRUE);
  if (!empty($fv['apachesolr_search']['retain-filters'])) {
    $get['retain-filters'] = '1';
    // Add the query values into the redirect.
    $form_state['redirect'] = array($form_state['redirect'], array('query' => $get));
  }
}

We are redirecting with a query argument. So all the filters will be visible in the URL. In your case I would not get the filters from the URL but from your function. And then send these filters to the submit function and you should be able to get what you want?

pwolanin’s picture

Status: Active » Fixed

It seems like you should just make custom search pages that pre-apply a filter?

Note that the search pages can take a wildcard arg such as a content type.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.