0

I would like to display a custom search form in my Drupal site.

The search form has just a textbox and a submit button beside it, no need of any hidden fields.

I tried with a simple html form in my template, that mimics the normal drupal search form:

<form accept-charset="UTF-8" id="search-form" method="post" action="/drupal/?q=search/page" class="search-form">
<div id="edit-basic" class="container-inline form-wrapper">
<div class="form-item form-type-textfield form-item-keys">
<input type="text" autocomplete="off" class="form-text" maxlength="255" size="40" name="keys" id="edit-keys" value="search" onBlur="if(this.value == '') { this.value = 'search'; }" onFocus="if(this.value == 'search') { this.value = ''; }" style="right:15px;">
</div>
</div>
<input type="hidden" value="form-Og2HsMomOhfRkoS262LFbqpN2NyIGeHHYfTF43Kynjs" name="form_build_id">
<input type="hidden" value="mfLsAR-y7CVQLcWY7hgZRGPbu3F3R5uQKKpZe3ppAi4" name="form_token">
<input type="hidden" value="search_form" name="form_id">
</form>

But that has got some problem like: “The form has become outdated. Copy any unsaved work in the form below and then reload this page.

Is there a way i can use a custom html form as Drupal Search form ? Basically i want to redesign the default Drupal search form, to suit my needs.

shasi kanth
  • 6,987
  • 24
  • 106
  • 158

1 Answers1

1

Use the default drupal search form and alter it as needed using hook_form_FORM_ID_alter

/** Implements hook_form_FORM_ID_alter */
function mymodule_form_search_form_alter (& $form, & $form_state)
{
  /* Do your alterations here */
}

Reference the Form API to help learn what changes you need to make.

Rplace mymodule in the code above with the name of your custom module. To output the form, in a template, use the below code:

$form = drupal_get_form('search_form');
print render($form);
tvanc
  • 3,807
  • 3
  • 25
  • 40
  • Well, i finally used a module_invoke code to show the search form in my template as a block: **** – shasi kanth May 24 '13 at 06:39
  • Why couldn't you just use the Blocks page at admin/structure/block to display the search-form block? I imagined, obviously incorrectly, that there was a reason that using a block was inappropriate in your case. – tvanc May 24 '13 at 22:51
  • Using module_invoke was one way, i also tried with theme_form_alter() in the template.php and made some custom changes to the form css. It worked well. Still accepting your answer, as it motivated me to look at the hook_form_alter(). – shasi kanth May 25 '13 at 05:57