Override the default Drupal 6 search form
Submitted by guvnor on Mon, 04/20/2009 - 09:06
Customising the Drupal 6 Search Form
The default search form or box in drupal is good - it is simple enough and works. However, it is not clear how to alter the look and feel and the default "Search this Site" label. This article describes how to do this. Out of interest the search form on this site was overridden in this manner. In this example I use a graphic image file as my submit button called "search_button.png" which I have placed in my theme directory under a folder called images.images/search_button.png
Step 1.
Copy a file called search-theme-form.tpl.php from the modules/search/ directory into your themes directory. This file won't be edited but it a copy must reside in your themes directory. For example if your theme is called mytheme you should copy the file to the this directoryStep 2
Next create a file called template.php within your template directory. This may already exist. The file should live heresites/all/themes/mytheme/
Step 3
Edit the template.php file and put a search form override function in. This function is listed below:<?php function phptemplate_preprocess_search_theme_form(&$variables) { /** * Changes to submit button */ // Remove the Text (I have removed it here as i am using a search button instead) //Note the t(''); means no text $variables['form']['submit']['#value'] = t(''); // add and image which replaces submit button $search_icon = base_path() . path_to_theme() .'/images/search_button.png'; // add 'src' attribute to submit button $variables['form']['submit']['#attributes'] = array('src' => $search_icon); // rebuild the rendered version (submit button, rest remains unchanged) unset($variables['form']['submit']['#printed']); $variables['search']['submit'] = drupal_render($variables['form']['submit']); /** * Changes to input field and it's label (title) */ // add default value to input field $variables['form']['search_theme_form']['#value'] = t('Search...'); // change label (title) of input field // remove label (title) of input field unset($variables['form']['search_theme_form']['#title']); // rebuild the rendered version (search form, rest remains unchanged) unset($variables['form']['search_theme_form']['#printed']); $variables['search']['search_theme_form'] = drupal_render($variables['form']['search_theme_form']); /** * Additional */ // collect all form elements to make it easier to print the whole form. $variables['search_form'] = implode($variables['search']); }