I'm trying to implement a search and results form in the admin interface but I am having trouble getting the search function and pager working.
[1] The pager is not showing at all. I've got this working using db_select for the query but I'd like to use a solr query. How can I get a pager to display?
[2] The form submission is not working as expected either. On submission the page is redirect to the correct url but an error message is displayed: The requested page '/admin/monitor/custom/search/?q=foobar'
could not be found. Do i have to declare a separate menu route /admin/monitor/custom/search/% to handle searches?
Many thanks,
function custom_menu(){
$items['admin/monitor/custom/search'] = array(
'title' => 'Custom',
'description' =>'Custom search form',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_form'),
'access arguments' => array('administer site configuration'),
);
return items;
}
function custom_form($form, &$form_state){
$table = build_results_table();
$form['keyword'] = array(
'#type' => 'textfield',
'#description' => t('Search database by keyword'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Go!'
);
$form['results'] = array(
'#markup' => $table,
);
return $form;
}
function build_results_table($search_term){
// perform query using Solr Search API
// page number from $_GET('page')
// search limit from a variable set by another module
$results = query_database();
$header = array('Firstname', 'Middlename', 'Surname');
$rows = //... build rows from $results ...;
$table = theme('table', array(
'header' => $header,
'rows'=>$rows,
));
pager_default_initialize($table);
$table .= theme('pager');
return $table;
}
function custom_custom_form_submit($form, &$form_state){
$keyword = $form_state['values']['keyword'];
drupal_set_message(t('Showing results for %keyword',
array('%keyword' => $keyword)));
$form_state['redirect'] =
'/admin/monitor/custom/search/?q='. $keyword;
}