Jump to Navigation

How to customise the search box in Drupal 6

Image to accompany story

By default, Drupal allows you to include a search box directly into your theme. In most themes, when enabled, this search box will show up in the primary navigation bar as an input box labeled "Search this site", with a submit button labeled "Search". But what if you want to alter or completely hide these labels, or even add a new class to the input box?

How to do it

There have been loads of suggestions about how to do this on drupal.org, including altering the core Drupal source (not a good idea!), using a string replace function in a custom template file or using the String overrides module. But, the best suggestion came from this post and is the most Drupal friendly way of doing it.

Use Drupal 6's preprocess function in your theme's template.php file to modify the template variables before they are passed into the template files.

/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/
 
function mytheme_preprocess_search_theme_form(&$vars, $hook) {
 
  // Modify elements of the search form
  $vars['form']['search_theme_form']['#title'] = t('Search mysite.com');
 
  // Set a default value for the search box
  $vars['form']['search_theme_form']['#value'] = t('Search');
 
  // Add a custom class to the search box
  $vars['form']['search_theme_form']['#attributes'] = array('class' => t('cleardefault'));
 
  // Change the text on the submit button
  $vars['form']['submit']['#value'] = t('Go');
 
  // Rebuild the rendered version (search form only, rest remains unchanged)
  unset($vars['form']['search_theme_form']['#printed']);
  $vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
 
  // Rebuild the rendered version (submit button, rest remains unchanged)
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);
 
  // Collect all form elements to make it easier to print the whole form.
  $vars['search_form'] = implode($vars['search']);
}

Note: you must rename the preprocess function so it's right for your theme (replace mytheme with the name of your theme)

In order for this to work, you must also ensure you have a copy of search-theme-form.tpl.php in your theme directory (you can grab it out of modules/search/ from your Drupal core files). You can then edit this as you like for further theming.

Extending it further.

The same techniques can also be applied for the search block. Just use the code from above, but replace all instances of search_theme_form with search_block_form, and make sure that you take a copy of search-block-form.tpl.php into your theme directory.

Comments

Anonymous's picture

Hey man. Thanks for this. Spent a couple days trying to figure how to get this working.

Anonymous's picture

Thanks a ton for this. It worked like a charm and also helped me to really understand "preprocess" functions. Is there a way to do the same with the "user login" block? I can try on my own - but I'm not sure where to find the original .tpl files.

Anonymous's picture

when I do this my submit button jumps under my input box. Any idea how to change that ?

Anonymous's picture

This is the most conceptually correct way to do it. Thumbs up! Looks great in my Firefox!

Anonymous's picture

Thank you =) It was very helpful as I'm new to Drupal.

Anonymous's picture

can this be done on a multilingual site?

Anonymous's picture

Just wanted to point out that if your working in Drupal 6 and have the following code:

function YOURTHEMENAME_preprocess_search_BLOCK_form(&$vars, $hook) {

// Set a default value for the search box
$vars['form']['search_theme_form']['#value'] = t('Search');

This will allow you to change the value of the search box. The code above didn't work. Just a quick change and it worked. Hope this helps someone.

Anonymous's picture

HUGE help, thanks so much for posting this.

Anonymous's picture

Thanks! This worked as soon as I flushed my Theme Registry. I added a line to customize the class for the submit button.

// Add a custom class to the submit button
$vars['form']['submit']['#attributes'] = array('class' => t('button'));

I was wondering how I could add the javascript for making the default value disappear when you click on the box.

Anonymous's picture

The very best method for that is a very little but powerful module : Custom Search Box. 5 seconds to configure it, without a line of code for the same result... Have fun !

Anonymous's picture

Nice module. That will come in handy for future sites. In a future release perhaps you could consider adding the ability to insert the javascript needed to create the disappearing default text when you click on the search box.

Anonymous's picture

I could not make it work. I am using drupal 6.11 and I probably missed something...

- I copy-pasted the code into template.php
- I changed the name to tendu_default_preprocess_search_theme_form - my theme is tendu_default under tendu parent theme.
- I copied search-theme-form.tpl.php to the sub-theme dir.

After this did not work, I also tried:
- to changed the name to tendu_preprocess_search_theme_form - the PARENT theme
- I copied search-theme-form.tpl.php to the parent theme dir.

Nothing...

Help? Thanks!

tom's picture

Amir: Did you clear your theme registry (cache)? You need to do this in order for Drupal to pick up the new template. There are 3 ways to do this:

  1. On the "Administer > Site configuration > Performance" page, click on the "Clear cached data" button.
  2. With devel block enabled (comes with devel module), click the "Empty cache" link.
  3. The API function drupal_rebuild_theme_registry.
Anonymous's picture

Thanks for the answer Tom.

Yes, I have cleared the cache (devel module). No help...

Anonymous's picture

I had to remove the second argument $hook to get this working.
This is identical to the number of arguments found in the
modules/search/search.module file.

Other than that its made customization a breeze.

Hope this helps anyone with a similar problem.

Anonymous's picture

very helpful... thanks. I was trying to read through that same post and with so many replies and opinions on 'the right way' it was hard to make sense of it. Now that I've learned a bit more, though, even this function that re-renders this search form - I've come to realize that even this solution isn't ideal, because it's causing a second render of the form. It would be best to affect the variables before it's rendered the first time and save cpu overhead, not that this is a bad solution: It's certainly better than waiting until the very last second before it hits the browser, basically, and doing a string replacement with an empty string..that just seems so hacky, to me. Of course, I have no idea how to do it(I'm assuming it is with hook_form_alter(),) but I'm learning. If I find a solution online to alter this before it's rendered the first time I will come back and post a follow-up link.

Thanks for this. It's been a key step to really learning what's going on with the way things are rendered for me.

-=- christopher

Anonymous's picture

For anybody that stumbled upon this, please also see:

http://drupal.org/node/224183

Anonymous's picture

Sorry for the lack of information there. That page is a great starting spot for anybody looking to modify their search form in Drupal 6 specifically. The comments in the thread eventually point to another excellent method here:

http://ahsangill.wordpress.com/2009/04/29/drupal-6-custom-search-box/

Also, for those with simple needs that just want to modify their search form without too much hassle, there's a module for that:

http://drupal.org/project/custom_search_box

Cheers!

Anonymous's picture

HI NICE STUFF....It's really very informative

Please share your thoughts, comments and suggestions...

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
If you have your own website, enter its address here and we will link to it for you. (please include http://).
eg. http://www.kirkdesigns.co.uk