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 . But, the best suggestion came from and is the most Drupal friendly way of doing it.
Use Drupal 6's in your theme's template.php file to modify the template variables before they are passed into the template files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 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.
On a similar note, you might be interested in my article on Customizing the comment form in Drupal 6.