Agaric Design Collective

Theme the Search Form in Drupal 6

By Dan Hakimzadeh
on 17 Nov
1 comment

Key words and phrases

how to theme search form in drupal 6 edit-submit form-text

Tags

Description

So it's 1am and you want to theme the search form output of your brand new drupal 6 theme, here's the quick and easy one step guide that will get you though it. Yep, that's right, 1 step, because that's how we do it the agaric way...

Resolution

STEP 1 of 1
Paste the code below into into your trusty template.php file and customize to your heart's desire... (leave out the php open and closing tags, we use em to make the output look nice, with pretty colors and stuff...)

<?php
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/

function mytheme_preprocess_search_theme_form(&$vars, $hook) {

 
// Modify elements of the search form
 
$vars['form']['search_theme_form']['#title'] = t('Search mysite.com');

 
// Set a default value for the search box
 
$vars['form']['search_theme_form']['#value'] = t('Search');

 
// Add a custom class to the search box
 
$vars['form']['search_theme_form']['#attributes'] = array('class' => t('cleardefault'));

 
// Change the text on the submit button
 
$vars['form']['submit']['#value'] = t('Go');

 
// Rebuild the rendered version (search form only, rest remains unchanged)
 
unset($vars['form']['search_theme_form']['#printed']);
 
$vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);

 
// Rebuild the rendered version (submit button, rest remains unchanged)
 
unset($vars['form']['submit']['#printed']);
 
$vars['search']['submit'] = drupal_render($vars['form']['submit']);

 
// Collect all form elements to make it easier to print the whole form.
 
$vars['search_form'] = implode($vars['search']);
}
?>

 

Extra info

If you want to theme the search block, when pasting the code above into template.php, change all occurrences of "search_theme_form" to "search_block_form" (not forgetting the function name itself).

If you want to delete the box label altogether, replace

$vars['form']['search_theme_form']['#title'] = t('Search mysite.com');

with

unset($vars['form']['search_theme_form']['#title']);

Posted by Richard (not verified) on Tue, 2008-11-18 16:27
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.