Customizing Search Block Form

In Drupal we can customize everything, even the search forms. This example show you how to make a custom search block. First you have to make a file named "search-block-form.tpl.php" with this content:

<div class="container-inline">
  <?php print $search_form; ?>
</div>

even if it isn't different from the one in the core distribution; otherwise the preprocess function isn't executed.

After that you must to open the template.php and use the mechanism of preprocess function to modify template variables before they are passed into the template files.

function phptemplate_preprocess_search_block_form(&$vars, $hook) {
        //modify title, size and button value of the search form
        $vars['form']['search_block_form']['#title'] = t('New title');
        $vars['form']['search_block_form']['#size'] = 24;
        $vars['form']['submit']['#value'] = "New Value";

        //Rebuild the rendered version
        unset($vars['form']['search_block_form']['#printed']);
        unset($vars['form']['submit']['#printed']);
        $vars['search']['search_block_form'] = drupal_render($vars['form']['search_block_form']);
        $vars['search']['submit'] = drupal_render($vars['form']['submit']);

        //Group all variables
        $vars['search_form'] = implode($vars['search']);
}

After that we can use the block section to add the block or use something like:

<?php
$block = module_invoke('search', 'block', 'view', 0);
print $block['content'];
?>

We can print all variables of the form to see the possibles values into the function (for testing use):

echo "<pre>";
print_r($vars['form']);
echo "</pre>";

0
Your rating: None
Anonymous's picture

Thank you !

Thanks you for this post. You are my hero.


Post new comment

  • 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><p>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [as], [as3], [css], [drupal5], [drupal6], [html], [java], [javascript], [mysql], [php], [xml].
  • You may quote other posts using [quote] tags.
  • Textual smileys will be replaced with graphical ones.

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.