How to theme the search form for Drupal 6 | Pixel Clever

Drupal is made to be customized; however some parts of Drupal theming don’t have much documentation written about them. Theming the search form is a good example. I don’t know if someone has added a tutorial in the last couple of months, but when I made the switch from Drupal 5 to Drupal 6 I couldn’t find a tutorial anywhere that explained how to do it, and the function that I had used for Drupal 5 was no longer working. So I tinkered with it a little, and after a bit of trial and error I came up with a method that works.

If you are updating your theme from Drupal 5 and you had a customized search form in that theme you should delete the theme function that called the search-theme-form.tpl.php file. The code in my old Drupal 5 themes looked like this:

function phptemplate_search_theme_form($form) {
/**    * This snippet catches the default searchbox and looks for  * search-theme-form.tpl.php file in the same folder  * which has the new layout.

*/

 

return _phptemplate_callback(‘search-theme-form’, array(‘form’ => $form));
}

Delete the code above if it is there in your template.php file.

If you are starting from scratch on a new Drupal 6 theme don’t add anything to your template.php. It’s not necessary. Drupal 6 picks up the tpl file automatically if it is in the theme directory.

Now in the folder for the theme you are working on create a new file called search-theme-form.tpl.php . In that file paste the following code:

<!–/there can’t be a space between the input field and the button or IE adds unwanted margins –>
<div class=”container-inline”>
<input type=”text” maxlength=”128″ name=”search_theme_form” id=”edit-search-theme-form-1″ size=”25″ value=”” title=”Enter the terms you wish to search for.” class=”form-text” /><input type=”image” src=”<?php print base_path() . path_to_theme() ?>/images/search.png” name=”op” id=”edit-submit-button” value=”Search” title=”Search” class=”form-submit” />
<input type=”hidden” name=”form_build_id” id=”<?php print drupal_get_token(‘form_build_id’); ?>” value=”<?php print drupal_get_token(‘form_build_id’); ?>”  />
<input type=”hidden” name=”form_token” id=”edit-search-theme-form-form-token” value=”<?php print drupal_get_token(‘search_theme_form’); ?>”  />
<input type=”hidden” name=”form_id” id=”edit-search-theme-form” value=”search_theme_form”  />
</div>

There are several items you need to pay attention to (and possibly edit) one of them is the path to the image “/images/search.png”. This path is relative to your theme folder, and the search.png can be any image file, but you have to create the image file yourself.

Also, once you create the image file and upload everything you might notice that the button is out of alignment. I have found that the best way to handle the alignment of the search button is by creating a div that surrounds the search button (you would do this in your page.tpl.php file) and then using relative positioning to put the button in its place. On this site the div I used has the class “searcher” and in my style sheets I used the following styles to pull it together

.searcher{
width:240px;
float:right;
margin-top:-6px;
}

.searcher .containter-inline {
margin: 0 0 0 0;
padding: 0 0 0 0;
}
.searcher .form-text {
margin:0 0 0 0;
padding:0 0 0 0;
width:165px;
}

#edit-submit-button {
position: relative;
top: 7px;
margin: 0 0 0 5px;
}

There you have it. That’s all it takes to theme the Drupal 6 search form.