Agaric Design Collective

Theme the Search Form in Drupal 6

Average: 3.6 (8 votes)
By Dan Hakimzadeh
on 17 Nov
5 comments

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
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.

Posted by Visitor (not verified) on Thu, 2009-10-01 07:05
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

Posted by Visitor (not verified) on Fri, 2010-01-01 04:05
Same issue

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

Posted by Ayush (not verified) on Fri, 2010-01-08 20:37
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

Posted by ZCJ (not verified) on Mon, 2010-02-01 17:53
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.