So I have the Search form block in my template, but after performing a search it doesn't show the query in the form field.

How do I set the default value on search results to be the query?

i.e if I search "cats"..on the search results box, I want "cats" to remain in the search box.

Just like it does on Drupal Answers :)

1 Answer 1


For the regular search block, the preprocessing function template_preprocess_search_block_form() runs (you can find it in the search.module file of the core search module), which assigns the search terms you've entered to the $key variable which is used by the function building the search form.

You should include your own preprocessing function (named appropriately to run before your own template file) which performs a similar task to initialize any variables you need/use (you can even grab the code from the one cited above). Since it sounds like you want to have the keywords printed, make sure the $key variable is actually set... If you have a custom theme, you can just include the preprocessing function in your theme's template.php file.

Does that help? Let us know if that works!

=========== ANSWER ADDITION ====================

OK, so here's what I've just tested that does work. I've added a search preprocessor to my custom theme in the template.php file. If you just copy this (with your own theme's name prefixed), it should work after you clear your cache.

I actually had to bang my head against the wall for a little bit. At first I just copied the preprocessing function exactly from the search module, with the exception of setting the field's value to the search term in that first line (since it doesn't do that be default)... But nothing rendered. Then I realized I had to add that second line I've commented on to set the '#printed' property to false, so that drupal_render() will render them again. Soooo... I don't know if this is a good solution or not. There may be a better way, but this is all I can think of and it works...

Again, in short, this is my copying the search module's preprocessing function and making the two line edits I've commented on by saying 'ADDED THIS LINE' to get the extra functionality. Hope that makes sense.

function YOUR_THEME_preprocess_search_block_form(&$variables) {
// ADDED THIS LINE -- get the search term from the URL, so you can use that as the value of the search box text field
  $variables['form']['search_block_form']['#value'] = arg(2);

  $variables['search'] = array();
  $hidden = array();
  // Provide variables named after form keys so themers can print each element independently.
  foreach (element_children($variables['form']) as $key) {
    $type = $variables['form'][$key]['#type'];
// ADDED THIS LINE -- these fields have already been rendered by the search module's preprocessing function, so they won't render again here unless you do this...
    $variables['form'][$key]['#printed'] = FALSE;
    if ($type == 'hidden' || $type == 'token') {
      $hidden[] = drupal_render($variables['form'][$key]);
    else {
      $variables['search'][$key] = drupal_render($variables['form'][$key]);
  // Hidden form elements have no value to themers. No need for separation.
  $variables['search']['hidden'] = implode($hidden);
  // Collect all form elements to make it easier to print the whole form.
  $variables['search_form'] = implode($variables['search']);  

p.s. The above would obviously make the change for all search block forms on your site...

  • Hey @Boriana any chance of expanding your explanation to include the preprocessing function to answer the question? Commented Oct 16, 2012 at 11:57
  • 1
    Alright, that took a little bit. The above is what I could get working. Hope it helps! Commented Oct 16, 2012 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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