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

crud I don't want to style). That requires me to get in the preprocess function:

* 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

kiteglinton’s picture

Hi netsensei, did you solve this one - it's puzzling me too

jhodgdon’s picture

Status: Active » Fixed

Is 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!

tonychung’s picture

Version: 6.12 » 6.15

This 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.php file 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'] = "
" . print_r( $vars['form'], true ) . "

";
}

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)

  1. Locate the keys you wish to alter and add declarations within the preprocess function.
      $vars['form']['search_block_form']['#title'] = t( 'Search' );
      $vars['form']['search_block_form']['#default_value'] = t( 'Search this site' );
      $vars['form']['submit']['#value'] = "";
  2. Add the following tags to render the form fields:
      unset($vars['form']['search_block_form']['#printed']);
      unset($vars['form']['submit']['#printed']);
      $vars['search']['search_block_form'] = drupal_render($vars['form']['search_block_form']);

    Note: I'm not entirely sure if we need to unset #printed. I'm only following other examples.

  3. Concatenate the rendered fields into a text string.
      $vars['search_form'] = implode( $vars['search'] );
  4. Copy the search-block-form.tpl.php file 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.php is the recommended solution.

  1. Copy the entire form_element() function from {drupal_install}/includes/form.inc into template.php.
  2. Rename the function {theme_name}_form_element($element, $value)
  3. Search for $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.

netsensei’s picture

Hi.

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!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Just had a discussion on IRC with ksenzee about this:

<ksenzee> When you add a preprocess function, you're just adding to the chain of preprocess functions
<ksenzee> not overriding anything.
<ksenzee> You're just sticking your function in there along with the others.
<BWPanda> Ah, like a hook_
<ksenzee> A bit like that.
<ksenzee> Whereas with a tpl or a theme function
<ksenzee> there is only one.

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