Domanda

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.

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top