Customizing the block search form
Description
This describes how to override the default BLOCK SEARCH THEME FORM* layout in phptemplate based themes and the search.module with Drupal 4.7 and Drupal 5.x.
* The BLOCK SEARCH THEME FORM is the search form that that appears in your sidebars, when enabled. Refer to the NOTES below for overriding the theme search form and main page search form.
Assumptions:
- Search Module is turned on.
- You are printing the search box from your theme (
<?php print $search_box; ?>or similar). - Search box is turned on in your theme configuration at /admin/build/themes/settings/YOUR_THEME_NAME.
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_block_form($form) {
/**
* This snippet catches the default searchbox and looks for
* search-block-form.tpl.php file in the same folder
* which has the new layout.
*/
return _phptemplate_callback('search-block-form', array('form' => $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-block-form.tpl.php file which determines the new layout.
A very simple example of a search-block-form.tpl.php file maybe illustrated as follows....
for use with Drupal 4.7.x
<label for="edit[search_block_form_keys]">Search</label>
<input type="text" maxlength="128" name="edit[search_block_form_keys]" id="edit-search_block_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-block-form" value="search_block_form" />
<input type="hidden" name="edit[form_token]" id="a-unique-id" value="<?php print drupal_get_token('search_block_form'); ?>" />Snippet updated and tested with Drupal 4.7.x Feb 24th 2007
for use with Drupal 5.x
<label for="search_block_form_keys">Custom Search</label>
<input type="text" maxlength="128" name="search_block_form_keys" id="edit-search_block_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-block-form" value="search_block_form" />
<input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_block_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-block-form.tpl.php file. Drupal automatically does that for you when it's rendering the form.
Upload your search-block-form.tpl.php file to your active theme folder.
example: changing the Search button text
Simply change the following line in the search-block-form.tpl.php snippet from this:
<input type="submit" name="op" value="Search" />
to this:
<input type="submit" 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-block-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).
Drupal 6.
To modify just the search block title, go to Administer > Blocks, find the search block in the list, click 'Edit' and insert the new title.
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 theme search form, have a look at the Customising the Theme Search Form handbook page.
- Please post tips/tricks discuss this handbook page at this thread.
Drupal 6 Tutorial
Drupal 6 tutorial (which is much simpler). (Also, for the general case see overriding themable output.)
