User login

Theme the Search Form in Drupal 6

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']);
}
?>

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

Comments

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']);

I tried to use the above

I tried to use the above could but it does not bring about any change in search form theme. I cant make out wat could be wrong. Please help.

Reply to seeing no change

If you see no change after some modification, think of clearing Drupal cache (then, reload your desired page)

To clear Drupal cache, go to Administer -> Site Configuration -> Performance, then scroll to bottom of page, finally click button Clear cached data

Now (after clear cached data), reload your desired page

Same issue

Hi. I changed the 'theme' to 'block' in the code, but was unable to see any changes. I did flush my cache.

some vars change, others don't?

Great concise post. I want to add a class to the form for a content type, but though I can change the title and default value of fields, the form class won't change the way I'm doing it.

I have a content type id 'testxx' with

function theme_preprocessor_testxx_node_form (&$form)

and when I set

$form['form']['#attributes'] = array( 'class' => t('addicition-interaction-list-form'));

no class is appended.

Where do I change the form class?

Thank you,
Z

@ Visitor: You'll have to

@ Visitor: You'll have to change "mytheme" in the beginning of the code to your theme's name. :)

Sort of works

I got this to work on the main search, but for the search results area the value of the search box remains changed but the submit button goes back to the original "Search"

Taken from

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.