Drupal Search Specific Form

To add a search form for specific content in any region of Drupal, use below code format.

/**

 * Implementation of hook_form_alter().

 */

function ch_form_alter($form, &$form_state, $formid) {    

  switch ($formid) {    

    case 'search_form':            

      if(isset($GLOBALS["search_form_default_type"])) {        

        $form["basic"]["#title"] = "";

        $form["basic"]["inline"]["keys"]["#default_value"] = isset($form["basic"]["inline"]["keys"]["#default_value"]) && ($form["basic"]["inline"]["keys"]["#default_value"]!='') ? $form["basic"]["inline"]["keys"]["#default_value"] : "Search Forum";

        $form["basic"]["inline"]["keys"]["#attributes"]["onclick"] = 'if($(this).val()=="Search Forum") {$(this).val("");}';        

        $form["advanced"]["type"]["#default_value"] = $GLOBALS["search_form_default_type"];

        $form["advanced"]["#prefix"] = "<div style='display:none;'>";

        $form["advanced"]["#suffix"] = "</div>";      }      

    break;

  }

}

/**

 * Implementation of hook_block.

 */

function snapvu_block($op = 'list', $delta = 0, $edit = array()) {

  switch ($op) {

    case'list':

      $blocks[13] = array('info' => t('Search Forum'));

      return $blocks;

      break;

    case'view':

      switch($delta) {

        case 13:

          $GLOBALS["search_form_default_type"] = array('forum');

          $content = drupal_get_form('search_form', $action = '', $keys = '', $type = 'node'); 

          unset($GLOBALS["search_form_default_type"]);        

          return array('subject'=>'<none>','content'=>$content);

        break;

      }

      break;

  }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: