Override the default Drupal 6 search form

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 directory

sites/all/themes/mytheme/search_theme_form.tpl.php

*Where path to your website is the path on the website where your drupal installation is kept.

Step 2

Next create a file called template.php within your template directory. This may already exist.

The file should live here

sites/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']);
}

Now with this in place you need to clear the cache of your drupal site

admin/settings/performance

And click the "Clear cached data" button.

If all has gone well you will see a modified search form.

Powered by Drupal, an open source content management system