diff --git a/apachesolr_multisitesearch.module b/apachesolr_multisitesearch.module index b075b3b..b99384f 100644 --- a/apachesolr_multisitesearch.module +++ b/apachesolr_multisitesearch.module @@ -463,3 +463,40 @@ function apachesolr_multisitesearch_apachesolr_delete_index_alter(&$query) { // Limit to the current site. $query = "($query) AND hash:" . apachesolr_site_hash(); } + +/** +* Implementation of hook_form_[form_id]_alter(). +* +* This adds spelling suggestions, retain filters to the search form. +*/ +function apachesolr_multisitesearch_form_search_form_alter(&$form, $form_state) { + if ($form['module']['#value'] == 'apachesolr_multisitesearch') { + $response = apachesolr_static_response_cache(NULL, 'apachesolr_multisitesearch'); + if (variable_get('apachesolr_search_spellcheck', TRUE) && $response) { + // Get spellchecker suggestions into an array. + if (isset($response->spellcheck->suggestions) && $response->spellcheck->suggestions) { + $suggestions = get_object_vars($response->spellcheck->suggestions); + if ($suggestions) { + // Get the original query and replace words. + $query = apachesolr_current_query(NULL, 'apachesolr_multisitesearch'); + + foreach ($suggestions as $word => $value) { + $replacements[$word] = $value->suggestion[0]; + } + $new_keywords = strtr($query->get_query_basic(), $replacements); + + // Show only if suggestion is different than current query. + if ($query->get_query_basic() != $new_keywords) { + $form['basic']['suggestion'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#type' => 'item', + '#title' => t('Did you mean'), + '#value' => l($new_keywords, $query->get_path($new_keywords)), + ); + } + } + } + } + } +}