How to create a custom search box

How to create a custom search box

Categories:

This is an example on how you can create a custom search box for your drupal site. The code below renders a text box and a button. When you perform a search, drupal will only look for the keyword inside the node types specified below (page, recipe, product, blog_article).

You can just copy and paste this code into a block.

<form action="/search/node" method="post" id="search-form" class="search-form">
<div class="form-item">
<input type="text" class="input-text" value="" size="25" name="keys" />
<input type="submit" value="Search" name="op" title="Search" alt="Search" />
<input type="hidden" value="<?php print drupal_get_token('search_form'); ?>" name="form_token" />
<input type="hidden" value="search_form" id="edit-search-form" name="form_id" />

<input type="hidden" name="type[blog_article]" id="edit-type-blog_article" value="blog_article" />
<input type="hidden" name="type[page]" id="edit-type-page" value="page" />
<input type="hidden" name="type[recipe]" id="edit-type-recipe" value="recipe" />
<input type="hidden" name="type[product]" id="edit-type-product" value="product" />
</div>
</form>

IMPORTANT NOTE! Since this approach hard-codes Advanced Search options in to hidden fields in the search form, to use the above approach, all users needing to search *must* have access to Advanced Search options. Sounds obvious, but thought I'd mention it.

Obviously, if it is not desired to expose Advanced Search options to some roles, you can always use hook_form_alter to unset the options in the search form for those roles.

Taken from: http://drupal.org/node/141788

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.