Changing the Search Block Form Text in Drupal 6
A couple of weeks ago I had a client request to change the search box on their site to have the text "SEARCH" appear inside of the text area on their search form. Some searching led me to a couple of posts on drupal.org about how to go about this, but the way that I chose ended up actually being in the comments on one of those posts. In short, what you do is put an override function into your theme that alters the search form before it gets displayed, setting the #value attribute of your search form's text field to the text you want. Additionally, there's some javascript added in so that the default text disappears when the visitor click in the text field.
Here's some example code that would go in your template.php file. It assumes that your theme is named "mytheme".
function mytheme_theme(&$existing, $type, $theme, $path) {
$hooks['search_block_form'] = array(
'arguments' => array('form' => NULL),
);
return $hooks;
}
function mytheme_search_block_form(&$form) {
// here's where we set our default search text.
$form_default = t('SEARCH');
$form['search_block_form']['#value'] = $form_default;
$form['search_block_form']['#attributes'] = array('onblur' => "if (this.value == '') {this.value = '{$form_default}';}", 'onfocus' => "if (this.value == '{$form_default}') {this.value = '';}" );
return drupal_render($form);
}
Comments
Hi, it doesn't work if you use a special character "ö" as search text. "Sök" = swedish for search. Do you have any idea why? BR/Daniel
Daniel, you replaced "SEARCH" in the example with "Sök"? I'm not sure why that wouldn't work. What happened when you did that?
The field gets empty. I tried using html entities (ö) but that didnt work either - it just outputed the hrml-entyties.
Are you sure this is only happening with the "ö"? What if you just leave it as "SEARCH"? It sounds like something else might be the problem.
Search works. Sok works, anything exept words with special characters - really strange.
My only guess would be a PHP problem with multi-byte (special) characters, but I'm not sure. Sorry I can't be of more help!
No worries, I'd check through the drupal code if I had the time. As for now I jsut installed the search-config module i found and it was cake :) Cheers
Add new comment