The requirement is that the user specifies a facet before they search. Here's what the search form should look like: enter image description here

Adding the select list to the search form is a straight forward form_alter(). My problem is how do I send the selected audience value from the drupal form to solr?

Once I have the audience value in the solr query object, I think I would apply the facet by using HOOK_apachesolr_query_prepare() (or HOOK_apachesolr_query_alter()).


  • I forgot to mention that the audience vocabulary is already configured as an apachsolr facet.
    – Brian Wood
    Apr 2, 2013 at 1:32

1 Answer 1


Here's my solution:

Add the select menu to the form and append a custom submit function

  function ucb_parking_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case "search_block_form":
      // Add the audience select list
      $form['search_audience'] = array(
       '#type' => 'select',
       //'#title' => t('Choose Audience'),
       '#options' => array(
      47 => t('Faculty/Staff'),
      36 => t('Student'),
       '#description' => t('Focus your search on an audience.'),
      //Add a custom submit function to the end of the array of functions.
      //(This function will run after apachesolr_search_form_search_submit().)
      array_push($form['#submit'], 'ucb_parking_front_page_search_submit');


In the custom submit function construct the solr url with a query string representing the facet that should be applied and redirect the user there:

function ucb_parking_front_page_search_submit($form, &$form_state) {
   * Simply construct the solr url for the search.  
   * Note the querystring for the facet must be passed via an array with the 'query' key
   * to ensure correct urlencoding.
  $form_state['redirect'] = array($form_state['redirect'], array('query' => array('f[0]' => 'im_field_audience:' . $form_state['values']['search_audience'])));
  //drupal_goto is probably equivalent
  //drupal_goto($form_state['redirect'], array('query' => array('f[0]' => 'im_field_audience:' . $form_state['values']['search_audience'])));

I think that this is the correct approach because apachesolr does pretty much the same thing with $form_state['redirect']:

 * 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'];
  // Replace keys with their rawurlencoded value
  if (isset($fv['search_block_form'])) {
    $raw_keys = str_replace("/","%2f",$fv['search_block_form']);
    $form_state['redirect'] = str_replace($fv['search_block_form'], $raw_keys, $form_state['redirect']);

Your Answer

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

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