I want to place a search box on the front page of a Drupal site I'm developing, but when I want to theme the search box, the HTML I'm getting from the preprocess function is incomplete.
This is what I do:
* I've copied search-block-form.tpl to my theme folder and changed the html (I'm using the $search_form variable)
* I've defined a region and activated the search block in that region.
I get the default search box block in my theme. Great, but I want to change the HTML (get rid of the
* I've gone to search.module and copied the template_preprocess_search_block_form(&variables) into my template.php
* I've changed the 'template_' prefix of the function to the name of my theme.
* I've flushed the cache in order to reset my theme registry.
When I reload the page, the search box is gone. I only get half of the HTML:
<div id="block-search-0" class="block block-search">
<h2>Search</h2>
<div class="content">
<form action="/<BASEPATH>/" accept-charset="UTF-8" method="post" id="search-block-form">
<div>
</div></form>
</div>
The form itself is rendered but the form elements are gone. I've checked the template preprocess function and noticed that drupal_render($variables['form'][$key]); returns NULL. I've dumped the variable and it does contain a form element array. I'm a bit confused about what happens here. Is something going wrong? Why is this working out of the box and not when I put the function in a seperate theme template.php?
My template.php contains only this preprocess function. Nothing else.
Any ideas?
Comments
Comment #1
kiteglinton commentedHi netsensei, did you solve this one - it's puzzling me too
Comment #2
jhodgdonIs this still an issue or did you solve it? I'm closing it as fixed for now, since it's a support request and has been there so long. Hopefully you went to the forums or IRC and got your problem solved (the issue queue is obviously not the best venue for support requests). Sorry about that!
Comment #3
tonychung commentedThis issue is closed, but I wanted to post how I troubleshot my version of the OP's problem, for the benefit of others puzzled by the same problem. Also, I would be interested in receiving feedback from Drupal experts who can show me a better way to do this.
Note: I installed the devel module so the system automatically clears the cache on each page refresh. This saves me a lot of time.
How to find the contents of the array that builds the search form
Add the following function to the
template.phpfile in your theme directory. Create the file if it doesn't exist.function {theme_name}_preprocess_search_block_form(&$vars, $hook) { // function to list all parameters $vars['search_form'] = "";
}
The output of the array appears when you view a page that includes the search box. For example:
/* relevant keys of $vars['form'] [search_block_form] => Array ( [#title] => Search [#type] => textfield [#size] => 15 [#default_value] => [#attributes] => Array ( [title] => Enter the terms you wish to search for. ) [#input] => 1 ... etc...How to customize the search block preprocess function
After discovering the construction of the array, create the actual preprocess function to perform the task. The following code is contained in
function {theme_name}_preprocess_search_block_form(&$vars, $hook)Note: I'm not entirely sure if we need to unset
#printed. I'm only following other examples.search-block-form.tpl.phpfile into your theme directory if you want to edit the output. I've seen examples where people take the raw array and build a new form completely from scratch.How to remove the trailing colon from the search box label
In my layout, the colon interferes with the design. There should be a setting to turn this on or off. Instead, creating an override function in
template.phpis the recommended solution.form_element()function from{drupal_install}/includes/form.incintotemplate.php.{theme_name}_form_element($element, $value)$t('!title: !required', and remove the colon so it reads$t('!title !required'The colon is removed from the label the next time the search box is run.
Constructing your own search form
I haven't dug deeply into this, but I would bet that rather than using the
drupal_render()function, you could manually compose the form using a combination of text, submit, and hidden fields.Further questions
I'm still having problems figuring out why the
$vars['form']['search_block_form']['#default_value']and$vars['form']['submit']['#value']don't modify the output values at all.Comment #4
netsensei commentedHi.
As far as I can recall: I didn't fix this one. I believe I just went along with the default search block and take care of it with some clever styling.
I need to take a look at this and see if this still occurs in the latest versions.
Thanks for the extensive answer, tonychung!
Comment #6
Anonymous (not verified) commentedJust had a discussion on IRC with ksenzee about this:
Therefore, you don't keep the original function's code in your overridden one, you start with an empty function and only add the code you want to change.
As tonychung describes above, you'll have to unset the '#printed' value and re-run druapl_render() to see your changes to template_preprocess_search_block_form().