Use Drupal 6's preprocess function in your theme's template.php file to modify the template variables before they are passed into the template files.
Note: you must rename the preprocess function so it's right for your theme (replace mytheme with the name of your theme)
In order for this to work, you must also ensure you have a copy of search-theme-form.tpl.php in your theme directory (you can grab it out of modules/search/ from your Drupal core files). You can then edit this as you like for further theming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /*** 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']);} |
In order for this to work, you must also ensure you have a copy of search-theme-form.tpl.php in your theme directory (you can grab it out of modules/search/ from your Drupal core files). You can then edit this as you like for further theming.
Searched words:
how to theme search form in drupal 6 edit-submit form-text
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...
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...)
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']);
}
?>
I came across a scenario where it was needed to customize Drupal 6 search box for my current drupal based theme and this customization was needed to :
Most recommendable and suitable way that i found was to create a new module and use form_alter hook to track the search form events and bring all above changes in these events.
I’ll recommend you to read this thread: http://drupal.org/node/214592 where Heine helped alot to get clear ideas about form_alter hooks and their usage. Hopefully it will help you too.
Now lets move towards the solution.
First of all create a new module under sites/all/modules with any name. Say it is named ‘abc’ (supposing that you will be aware of the process of module creation and all files needed to create for a new module in Drupal). Open your abc.module file and create a new function there named ‘abc_form_alter‘ having parameters (&$form, $form_state, $form_id). Basically, ‘abc_form_alter‘ will implement hook_form_alter (explained well http://drupal.org/node/214592) and will use this function to grab search form events and implement my search box changes on triggering of those events. So this is the summary of our solution and following code you’ll write in abc.module file.
.
Lets make a short review on above code,
This condition if($form_id==’search_theme_form’) will check if ‘search_theme_form’ is about to render then do following stuff:
One last thing, if you’r at very basic level in Drupal or even not a programmer then i’ll recommend you to go for this module Custom Search Box. This module is good but keep in mind that you won’t have everything in your hand while using this module but above process can help you to play freely with search form attributes.
I hope this post will open new horizons for you to customize any Drupal based Form
Resolution
STEP 1 of 1Paste 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']);
}
?>
I came across a scenario where it was needed to customize Drupal 6 search box for my current drupal based theme and this customization was needed to :
- Change Default text on submit button
- Change Default text in search box (Search this site)
- When user clicks on search box, default search box text should disappear
Most recommendable and suitable way that i found was to create a new module and use form_alter hook to track the search form events and bring all above changes in these events.
I’ll recommend you to read this thread: http://drupal.org/node/214592 where Heine helped alot to get clear ideas about form_alter hooks and their usage. Hopefully it will help you too.
Now lets move towards the solution.
First of all create a new module under sites/all/modules with any name. Say it is named ‘abc’ (supposing that you will be aware of the process of module creation and all files needed to create for a new module in Drupal). Open your abc.module file and create a new function there named ‘abc_form_alter‘ having parameters (&$form, $form_state, $form_id). Basically, ‘abc_form_alter‘ will implement hook_form_alter (explained well http://drupal.org/node/214592) and will use this function to grab search form events and implement my search box changes on triggering of those events. So this is the summary of our solution and following code you’ll write in abc.module file.
function abc_form_alter(&$form, $form_state, $form_id) {
if($form_id=='search_theme_form')
{
$form['submit'] = array('#type' => 'submit', '#value' => t('[ GO ]'));
$form['search_theme_form']['#default_value'] = 'Search My Site';
$form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );
}
}and That’s All Lets make a short review on above code,
This condition if($form_id==’search_theme_form’) will check if ‘search_theme_form’ is about to render then do following stuff:
$form['submit'] = array('#type' => 'submit', '#value' => t('[ GO ]'));Above Line will change the default submit button text and make it to be [ GO ] $form['search_theme_form']['#default_value'] = 'Search My Site';This code Line will Change Default text in search box and make it to be ‘Search My Site’ $form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );This final code line gives the way to change any attribute for this text box (you may use it to change the default css class used for search box, too). Here i changed the ‘onfocus’ attribute and wrote the javascript code so when user click on search box for searching some keyword, search box’s default text (Search My Site) should get vanished.One last thing, if you’r at very basic level in Drupal or even not a programmer then i’ll recommend you to go for this module Custom Search Box. This module is good but keep in mind that you won’t have everything in your hand while using this module but above process can help you to play freely with search form attributes.
I hope this post will open new horizons for you to customize any Drupal based Form
No comments:
Post a Comment