Drupal: How to Redefine $search_box Output
This article is devoted to changing of the standard search box which is passed to .tpl.php through $search_box variable.
Solution
Show a form in template
You need insert the following code:
<?php if ($search_box): ?><div class="block block-theme"><?php print $search_box ?></div><?php endif; ?>
Search form will be shown when:
- Search standard module is activated
- Search form showing is enabled (‘Search box’ is checked) on the page of theme settings (admin/build/themes/settings/< theme-name >)
- Права доступа к форме поиска ("Поиск материалов") были установлены на странице "Контроль доступа" (admin/user/access) в зависимости от роли.
Change search form appearance
Step 1
- Create template.php file if there is no such file in your theme.
- Add the following code there:
<?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’));
}
?>
Step 2
Snippet in template.php catches search form by default before its showing and finds search-theme-form.tpl.php in the same folder. This file defines new graphical design.
Very simple example of search-theme-form.tpl.php file (Druapl 4.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 is updated and checked for compatibility with Drupal 4.7.x and 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’); ?>" />
Attention:
You don’t need opening and closing tags in the search-theme-form.tpl.php. Drupal will when forms drawing.
Example. Changing text of "Search" button
Find the following line in search-theme-form.tpl.php:
<input type="submit" name="op" value="Search" />
and change it to:
<input type="submit" src="images/go-button.gif" name="op" value="GO!" />
Example. Replacing search button with a picture
You can do this using CSS. But if you wish to replace button with an image using your search-theme-form.tpl.php then you need to replace this code:
<input type="submit" name="op" value="Search" />
with this one:
<input type="image" src="images/go-button.gif" name="op" value="Search" />
Set correct path to the picture.