Print a form, such as a search form, anywhere in Drupal

Key words and phrases: 
form anywhere print form print search form drupal print the searchform
search_form_in_page.png
Description & Info: 

Resolved error:

Missing argument 1 for search_box(), called in /sites/example/www/includes/common.inc(1352) : eval()'d code on line 5 and defined in /sites/example/www/modules/search/search.module on line 1037.

This was from code added to a page's content by a client or possibly even the previous developer. We completely redid the site but did copy-paste in some pages.

I'd like to just print the form, so go looking for a search form function... ah! http://api.drupal.org/api/function/search_form/5 Who would have thought it.

As shown in the screen shot, this code in a node (or block) with PHP enabled under "Input format" should work beautifully:

<div class="searchblock">
<?php
print drupal_get_form('search_form');
?>

</div>

This is the same approach used in:
Display a Drupal login form anywhere with PHP, including in page content

Postscipt: Looking at search_box() --

http://api.drupal.org/api/function/search_box/5

<?php
function search_box($form_id) {
 
// Use search_keys instead of keys to avoid ID conflicts with the search block.
 
$form[$form_id .'_keys'] = array(
   
'#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['#base'] = 'search_box_form';

  return
$form;
}
?>

-- it seems that with any made-up form id it would work?

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> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links 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.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.