Customising the search forms
description
This describes how to override the default SEARCH THEME FORM* layout when using phptemplate based themes and the search.module with Drupal 4.7 and Drupal 5.x.
* The SEARCH THEME FORM is the Search form that that appears in your page header, when enabled. Refer to the NOTES below for overriding the block search form and main page search form.
You may find it easier to just edit the page.tpl.php file with your customised theme search form layout, but, here is an alternate method if you prefer to keep it seperate.
Step 1 of 2
In a text editor like notepad.exe, create a file called template.php using the the following snippet. If you already have a template.php file, simply add it to your existing one.
<?php
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), array('search-theme-form'));
}
?>Upload your new/edited template.php file to your active theme folder.
Step 2 of 2
The template.php snippet catches the default search box layout before it's displayed and looks in the same folder for a search-theme-form.tpl.php file which determines the new layout.
A very simple example of a search-theme-form.tpl.php file maybe illustrated as follows....
for use with Drupal 4.7.x
<label for="edit[search_theme_form_keys]">Search</label>
<input type="text" maxlength="128" name="edit[search_theme_form_keys]" id="edit-search_theme_form_keys" size="25" value="" title="Enter the terms you wish to search for." class="form-text" />
<input type="submit" name="op" value="Search" />
<input type="hidden" name="edit[form_id]" id="edit-search-theme-form" value="search_theme_form" />
<input type="hidden" name="edit[form_token]" id="a-unique-id" value="<?php print drupal_get_token('search_theme_form'); ?>" />Snippet updated and tested with Drupal 4.7.x Feb 24th 2007
for use with Drupal 5.x
<label for="search_theme_form_keys">Custom Search</label>
<input type="text" maxlength="128" name="search_theme_form_keys" id="edit-search_theme_form_keys" size="25" value="" title="Enter the terms you wish to search for." class="form-text" />
<input type="submit" name="op" value="Search" />
<input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form" />
<input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_theme_form'); ?>" />Snippet updated and tested with Drupal 5.x Feb 24th 2007
Note: You don't need the opening and closing form action tags in your search-theme-form.tpl.php file. Drupal automatically does that for you when it's rendering the form.
Upload your search-theme-form.tpl.php file to your active theme folder.
example: changing the Search button text
Simply change the following line in the search-theme-form.tpl.php snippet from this:
<input type="submit" name="op" value="Search" />
to this:
<input type="submit" src="images/go-button.gif" name="op" value="GO!" />
Which changes the button text from "SEARCH" to "GO!". You can obviously edit the Value="GO!" field to whatever text string you want.
example: changing the Search button to an image
This can be done using CSS, but, if you want to change the button to an image using your search-theme-form.tpl.php, you can simply change the following line:
from this:
<input type="submit" name="op" value="Search" />
to this:
<input type="image" src="images/go-button.gif" name="op" value="Search" />
This replaces the SUBMIT button with an image called go-button.gif. Edit the path and filename to suit).
Notes
- Thanks to Steve Temple, Jeff H and Philk for helping out with the finalised snippets.
- Name the classes or change the layout to whatever you want.
- To override the block have a look at the Customising the Search Block Form handbook page
- Please post tips/tricks discuss this handbook page at this thread.
- Updated Feb 24th 2007 by Dublin Drupaller - the snippets above have been tested with Drupal 4.7.6 and Drupal 5.1
