I've added a dropdown to Drupal search page interface. I would like for users to be able to select this control to sort results according to Date or Keyword Relevance.

I'm able to get the dropdown value in a submit handler, however, I don't know how to programmatically alter the weight of 'keyword relevance' and 'date'.

Here is my submit handler. Any help would be greatly appreciated.

function test_sort_form_submit_handler($form, &$form_state) {
  drupal_set_message("Drop Down Value Is: " . $form_state['values']['search_sort']);
  • Have you tried altering the form? I suppose you alter it to add the dropdown, so add #weight value to other form elements. – sanzante May 14 '16 at 21:22
  • I would just have two pages with different views and link them. – Niall Murphy Feb 3 '17 at 17:20

I would do this with a view instead of trying to tweak Drupal's search page:

  1. Create a view
  2. Add the filter "Search: Search Keywords" and expose it
  3. Add two sort criteria: your date field and the "Search: Score" field and expose both of them
  4. In Advanced > Exposed form > settings, check "Allow people to choose the sort order" if you want to give people that option
  5. Add the fields or setup the display as you prefer

You can set the default sort option by ordering the sort criteria and the asc/desc setting by choosing that option for the top criteria. You can also use the Better Exposed Filters module (full disclosure: I'm the maintainer) to combine the sort field and direction into a single dropdown and/or rewrite the text of the options.

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.