<div class="fr_search">        
  <form action="/"  accept-charset="UTF-8" method="post" id="search-theme-form">
    <input  name="search_theme_form" id="edit-search-theme-form-1" size="15" value="" class="form-text" />
    <!-- … -->

The above code is generated by the Drupal Search module. I want to add this code to the form.

<select class="search_l" onchange="selectSearch(select)">
  <option value="0">whole site search</option>
  <option value="1">google search</option>

How do I do this?


I would rather implement hook_form_FORM_ID_alter() to alter the form returned by search_box(). If mymodule.module is the name of your module, then you should add the function mymodule_form_search_box_alter(&$form, &$form_state). You need also to replace the form submission handler with your own.

function mymodule_form_search_box_alter(&$form, &$form_state) {
  $form['submit']['#weight'] = 10;

  $form['search_type'] = array(
    '#type' => 'radios', 
    '#options' => array(t('Whole site search'), t('Google search')),
    '#default_value' => 1,
    '#weight' => 5,

  // Replace the search.module handler with your own.
  if (in_array('search_box_form_submit', $form['#submit'])) {
    $key = array_search('search_box_form_submit', $form['#submit']);
  array_unshift($form['#submit'], 'mymodule_search_box_submit');

You could also show the form field containing the text to search inline with the radios you add.
The code I reported replaces the form submission handler with mymodule_search_box_submit() that must be implemented from your own module. This is the code executed from the submission handler implemented by search.module; I report it as reference.

function search_box_form_submit($form, &$form_state) {
  // The search form relies on control of the redirect destination for its
  // functionality, so we override any static destination set in the request,
  // for example by drupal_access_denied() or drupal_not_found()
  // (see http://drupal.org/node/292565).
  if (isset($_REQUEST['destination'])) {
  if (isset($_REQUEST['edit']['destination'])) {

  $form_id = $form['form_id']['#value'];
  $form_state['redirect'] = 'search/node/' . trim($form_state['values'][$form_id]);

As reference, I report here the code of the search.module function that builds the search box form.

function search_box(&$form_state, $form_id) {
  $form[$form_id] = array(
    '#title' => t('Search this site'), 
    '#type' => 'textfield', 
    '#size' => 15, 
    '#default_value' => '', 
    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  $form['#submit'][] = 'search_box_form_submit';

  return $form;
  • which file should i put the function search_box(){} in. i can't understand the code under Replace the search.module handler with your own.well, could you give me an explaination .thank you
    – enjoylife
    Mar 20 '11 at 2:42
  • 1
    search_box() is the function that builds the form, and it is defined in search.module; it doesn't need to be replaced. The code that replaces the form submission handler is doing exactly that: it replaces the submission handler defined from the Search module with mymodule_search_box_submit() (change the function name with the name of the function defined from your module). What you need to do is to create your own module that implements hook_form_search_box_alter() and the submission handler.
    – apaderno
    Mar 20 '11 at 12:46
  • If the name of your module is, e.g., custom_module then you need to replace mymodule_ in the code I reported with custom_module_.
    – apaderno
    Mar 20 '11 at 12:46

The best way to do this, IMO, would be to simply use hook_form_alter().


I think you are better off using hook_form_alter() than editing the template (no offense, but that is really bad practice).

You probably want to add a new form element and a submit handler to the search form, so you can handle the submit action and process the value of the new element.

  • beat me to it by a couple seconds :)
    – Alex Weber
    Mar 19 '11 at 15:40
  • I agree!! Will delete my answer to not keep bad solutions around, not sure why theme override came to my mind first.
    – Dipen
    Mar 19 '11 at 17:33

There is a search-block-form.tpl.php template:


In that template it's important to have hidden fields. Mine currently looks like:

<form action="" method="post" class="search_form">
      <?php print $search['hidden']; ?>
        <input type="text" name="search_block_form" class="input" />
        <input type="submit"  id="edit-submit--3" name="op" value="" class="submit" />

This is great for just the search box but the results displays on the main search page, which I need to override. For that reason I want to be able to use the search-result(s).tpl.php and block files. But I can't seem to get the code correct in my template file. I tried using the code on this page changing obviously the theme template but the changes I made to the tpl.php shows they are not being used. What am I doing wrong?

function dove_preprocess_search_results(&$variables) {
  $variables['search_results'] = '';
  if (!empty($variables['module'])) {
    $variables['module'] = check_plain($variables['module']);
  $index = 0;
  foreach ($variables['results'] as $result) {
    $variables['search_results'] .= theme('search_result',
        'result' => $result,
        'module' => $variables['module'],
        'extra_classes' => $index % 2 === 0 ? ' even' : ' odd',
  $variables['pager'] = theme('pager', array('tags' => NULL));
  $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];

I just thought I'd add the reason I need to edit the files is I'm using custom search module which is great but in the result it's giving me the type:allthecontenttypes in the input form plus the search results. U G L Y.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.