Changing the Search Block Form Text in Drupal 6

by Chadwick Wood
August 6th, 2009

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);
}