1

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;

}
1
  1. Drupal only has only pager implementations for DB queries, but you can use the pager system manually for data in external sources. There are a couple of examples on the pager_default_initialize() docs that should get you started.

  2. Anything other than q as the param name should work - Drupal uses q as the path param in its rewrites, so it's probably trying to find a route of foobar in your example.

  • Thanks, [1] I got the pager to display by adding the correct parameters to pager_default_initialize($count, $limit). However I now have an issue of incorrect 'page' path parameters as described here: drupal.stackexchange.com/questions/189708/…. The first page results are displayed on both page 1 and 2. [2] I replaced 'q' as a path parameter with 'search' and now it works. Cheers! – RobotEyes Feb 8 '16 at 18:12
  • "Incorrect" to you (and me as it happens), but not to Drupal! That's a long standing issue of contention, I believe the only answer is to roll your own pager implementation. Didn't even make it into D8 as it happens: drupal.org/node/1818040 – Clive Feb 8 '16 at 18:16
  • Oh no! Is there not a way of changing the links in a pager preprocess function? – RobotEyes Feb 8 '16 at 19:07
  • Links, yes, but not the functionality that handles them; it's hard-coded to start from zero in at least pager_default_initialize(), probably others too (other functions probably rely on zero-based indexes without adjustment too). I think that's why the only advice I've seen is to implement your own mechanism – Clive Feb 8 '16 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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