Skip to: Site menu | Main content

Additional Search Blocks in Drupal

Ever need to create additional search blocks for your Drupal installation? One use might be so that you can theme or position the block differently depending on which page the block appears on.

It's actually quite easy to do - all you need to do is create a new module that implements "hook_block" and then create a new block and call the "search_box" function (from search.module) to populate the box with the correct form elements. Here's the code:

function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Quicktips Search form');
    return $blocks;
  }
  else if ($op == 'view') {
    switch($delta) {
      case 0:
        if (user_access('search content')) {
          $block['content'] = search_box('search_block_form');
          $block['subject'] = t('Search');
        }
        break;
    }
    return $block;
  }
}

In this case, the parameter "search_block_form" is the form_id that will be assigned to the search form. Once you install the module, you'll be able to go to the admin area and place your new search block anywhere you'd like.
Submitted by michael on Tue, 08/07/2007 - 5:31pm.
Filed under:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <table> <tr> <td> <th> <div> <span> <p> <br> <blockquote> <hr>
  • Lines and paragraphs break automatically.

More information about formatting options

Please wait...
He that can have patience can have what he will.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.