2

I want to add in my D7 installation a search field inside a special part of the page, and I don´t want to use the default search block (as it will be harder to theme, this box I have already worked on it).

Naturally, if I just type:

<form><input type="text" placeholder="Search"></form> 

it won´t work.

What else does this need to actually act like the default search form in the search block? I know there´s this Custom Search module, but I think that I shouldn´t install yet another module for this simple functionality.

Thanks!!

2
  • 4
    is there any functional change to the search box? that is is the search box going to out put some thing different than the default search box. and when you say harder to theme what do you mean? Commented Jan 28, 2013 at 5:09
  • Hi!! The search box should output and behave exactly the same as the default search box, I want to completely replace it. I wanted to have a search box at the top (as stackoverflow does), and it´s easier to just paste up there a textbox than configure the search box to appear the way I want. So I thought maybe I should just create another searchbox... Thanks!!
    – Rosamunda
    Commented Jan 28, 2013 at 11:21

4 Answers 4

8

You could simply call drupal_get_form() on the search form_id, which I think is - either search_block_form or search_form

And use drupal_render() on the returned array.

  print drupal_render(drupal_get_form('search_block_form'));

Though I might be wrong about the drupal_render() part, yet it seems to me since the drupal_get_form() returns an array drupal_render should be used on it.

Also this page might be helpful too - http://data.agaric.com/print-form-such-search-form-anywhere-drupal

Hope that helps.

0
4

I'm not sure why you said the default search box is harder to theme. Drupal can do theme override or form override as you like. If you google, you will see many solutions for this purpose.

Check this Q/A : how to overwrite the search box? and you could also override search-block-form.tpl.php. Then, you can define which pages the search block to be appeared at the block configuration page admin/structure/block/manage/search/form/configure

OR remove the search block from any region and then you can print the block programmatically where you want. See Placing the contents of a block in any location.

<?php       
$block = module_invoke('search', 'block_view', 'form');
print render($block['content']);
?>
4
/*
 * create search block
 */

function mymodule_block_info() {
$blocks['my_search'] = array(

'info' => t('My Search Block'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function mymodule_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'my_search':
  $block['title'] = 'MyCustom Search';
  $srchblk = drupal_get_form('mymodule_search_block_form');
  $block['content'] = drupal_render($srchblk);
  break;
  }
  return $block;
}

/* create search form */

function mymodule_search_block_form($form, &$form_state) {
$form['keys'] = array(
'#type' => 'textfield',
'#size' => 15,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Search',
);
return $form;
}


/*
 * create search result menu callback
 */

function mymodule_menu() {
$items['custom/node/search/%'] = array(
'page callback' => 'mymodule_do_search',
'page arguments' => array(3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}

/*
 * create form handler
 */

function mymodule_search_block_form_submit($form, &$form_state) {
drupal_goto('custom/node/search/' . $form_state['values']['keys']);
}
 /*
  * create search result callback function
  */ 


function mymodule_do_search($keys) {
  // set the page title

drupal_set_title('MY search results');
  // get the search results

 $results = module_invoke('node', 'search_execute', $keys );
 return theme('search_results', array('results' => $results));
 }
0

This is what I needed:

$block = module_invoke('search', 'block_view', 'form');
print render($block['content']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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