function apachesolr_search_form_search_form_alter in Apache Solr Search 6.2
Same name and namespace in other branches
- 6 apachesolr_search.module \apachesolr_search_form_search_form_alter()
Implementation of hook_form_[form_id]_alter().
This adds spelling suggestions, retain filters to the search form.
File
- ./
apachesolr_search.module, line 1195 - Provides a content search implementation for node content for use with the Apache Solr search application.
Code
function apachesolr_search_form_search_form_alter(&$form, $form_state) {
if ($form['module']['#value'] == 'apachesolr_search') {
$form['#submit'] = array(
'apachesolr_search_form_search_submit',
);
// No other modification make sense unless a query is active.
// Note - this means that the query must always be run before
// calling drupal_get_form('search_form').
$apachesolr_has_searched = apachesolr_has_searched();
$queryvalues = array();
if ($apachesolr_has_searched) {
$query = apachesolr_current_query();
$queryvalues = $query
->get_url_queryvalues();
}
$form['basic']['apachesolr_search']['#tree'] = TRUE;
$form['basic']['apachesolr_search']['queryvalues'] = array(
'#type' => 'hidden',
// We use JSON encoding instead of PHP serialize, since otherwise we be
// at risk of user input being injected into the hidden string and
// unserialized.
'#default_value' => json_encode($queryvalues),
);
$form['basic']['apachesolr_search']['get'] = array(
'#type' => 'hidden',
'#default_value' => json_encode(array_diff_key($_GET, array(
'q' => 1,
'page' => 1,
'filters' => 1,
'solrsort' => 1,
'retain-filters' => 1,
))),
);
if ($queryvalues || isset($form_state['post']['apachesolr_search']['retain-filters'])) {
$form['basic']['apachesolr_search']['retain-filters'] = array(
'#type' => 'checkbox',
'#title' => t('Retain current filters'),
'#default_value' => (int) isset($_GET['retain-filters']),
);
}
if (variable_get('apachesolr_search_spellcheck', TRUE) && $apachesolr_has_searched && ($response = apachesolr_static_response_cache())) {
// 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();
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' => '<div class="spelling-suggestions">',
'#suffix' => '</div>',
'#type' => 'item',
'#title' => t('Did you mean'),
'#value' => l($new_keywords, $query
->get_path($new_keywords)),
);
}
}
}
}
}
}