Customizing 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 customized 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 customizing 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
Styling Drupal 5.x search forms
Please test on Test-Dev-Site before applying in Live-Production Site
If you are working on a project that requires you to apply a fancy pants style to a Drupal search form, you may thought this would be simple enough, as it’s pretty easy overriding the default themes, but everything else in Drupal.
At long last, we try creating a theme function named mytheme_search_form(). Create the above-named function and gave it the following definition:
<?php
function mytheme_search_form($form){
return _phptemplate_callback('search_theme_form', array('form' => $form));
}
?>Then create a file in your template directory named “search_theme_form.tpl.php” and built a custom form.
Next we can add additional search fields to the form. To do this, we look at node.module, where the function node_form_alter() adds fields to the default form. It didn’t seem necessary to jump through that hoop since the form is mostly hard-coded anyway, so we just add some radio buttons with the name “category” so that we can filter search results by taxonomy. Simple enough. But the search will never actually filter on results. Here we do more hair-pulling and weird debugging. Finally, we go back to the hook_form_alter() functionality, noticing a “processed_keys” key in the $form array. So to make the form honor category search, we add the following things to your template.php file. It’s not clear how much of this is necessary, and we rather suspect we are doing something stupid here, but it seems to work and we are on deadline, so we roll with it.
<?php
function mytheme_search_keys($type = null){
$keys = search_get_keys();
if($type){
$keys = search_query_insert($keys, 'type', $type);
}
if($_POST['category']){
$keys = search_query_insert($keys, 'category', $_POST['category']);
}
return $keys;
}
/* Taking over this function so that we can call mytheme_search_validate to do the advanced search.*/
function blog_form_alter($form_id, &$form){
if($form_id == 'search_form'){
$form['#validate']['mytheme_search_validate'] = array();
}
}
/* Need to call this to add category to the processed_keys array item so that
* the category actually gets searched in the mini-advanced form we generate
* in mytheme_search_form().
*/
function mytheme_search_validate($form_id, $form_values, &$form){
$keys = $form_values['processed_keys'];
$keys = mytheme_search_keys($form_values['module']);
form_set_value($form['basic']['inline']['processed_keys'], $keys);
}
function faq_search($op = 'search', $keys){
switch($op){
case 'name':
return t('content');
default:
$keys = mytheme_search_keys('faq');
return node_search('search', $keys);
}
}
function forum_search($op = 'search', $keys){
switch($op){
case 'name':
return t('content');
default:
$keys = mytheme_search_keys('forum');
return node_search('search', $keys);
}
}
function blog_search($op = 'search', $keys){
switch($op){
case 'name':
return t('content');
default:
$keys = mytheme_search_keys('blog');
return node_search('search', $keys);
}
}
?>Now for an explanation. The "mytheme_search_keys()" function is a helper that lets us make sure we are limiting the search to a given node type. It's not clear if that is absolutely necessary, but things seem not to work if we don’t add the “type:” string to the search, so we are leaving it in. Note that this function also looks for $_POST['category'] and adds it to the keys. If we wanted to add other search fields, we would add them here as well. we suppose that since mytheme_search_validate() calls this function to set keys, we could eliminate the extra function and just do the same work in mytheme_search_validate().
Next up, blog_form_alter(). The hook_form_alter() functions are associated with modules, so we chose one we knew our site would be using that didn't have a form_alter hook defined already. It feels kind of hacky, but it seems to work. The idea here is that we need to make the form run a validation function in order to add the keys we’re pulling in from mytheme_search_keys(). It was when we added this code that the category filter actually started working, so it seems to be a crucial bit. The key seems to be adding the keys to the $form['basic']['inline']['processed_keys'] array item, which seems to handle adding the search criteria to the URL and to the search itself.
Finally, we added the three _search() functions, which again feel a little extraneous, but the thing doesn't work unless we add them, so they're staying put for now. All we're doing in these functions is adding the node type to the keys being searched (the search code extracts things like “type:blog” and “category:3″ from the query to do advanced searches) and then executing the node_search() function with the revised $keys value.
So, there you have it, the long way around to having a custom-themed Drupal search form with additional filters based on node type and category.
Note: Thanks to Two Ells

Doing this in Drupal 6
This post should be updated with a link to the Drupal 6 tutorial (which is much simpler). (Also, for the general case see overriding themable output.)