In the past, if you wanted to manually embed the search form into your template you could use print $search_box;. However, this was removed from Drupal 7.

So, I'm asking what is the best method to do this in Drupal 7, now?

1) I know you can use regions/blocks for this, but that's not my current goal
2) I tried to use $block = module_invoke('search', 'block', 'view', 'search'); print $block['content'];, but it didn't seem to do the trick
3) This process worked: drupal_render(drupal_get_form('search_block_form'));. Is this the best method?

So, is there a best practice to use for embedding blocks (namely the Search Block) in Drupal 7? I've always used module_invoke in the past, but it doesn't seem to be as cooperative in Drupal 7. If I'm approaching this completely wrong, I'd appreciate any advice to follow the best way in Drupal.

Comments

dgastudio’s picture

$block = module_invoke('block', 'block_view', 1);

print $block['content'];
?>
dax444’s picture

I was able to get this working by doing the following:

          $block = module_invoke('search', 'block_view', 'search');
          print render($block); 
iamacyborg’s picture

That also worked for me. Thanks!

waqarit’s picture

That's really cool and worked for me from starting of Drupal 7.

davidneedham’s picture

The most flexible solution I've found is to re-add the $search_box variable. We do that by preprocessing page to include our beloved variable. Just add this snippet to template.php:

<?php
/*
 *  Preprocess page.tpl.php to inject the $search_box variable back into D7.
 */
function template_preprocess_page(&$variables){
  $search_box = drupal_render(drupal_get_form('search_form'));
  $variables['search_box'] = $search_box;
}

Don't forget to replace 'template' with the name of your theme.

Hat tip to @jenlampton for the drupal_render magic

--
David Needham

AlonGoldberg’s picture

@davidneedham - Thanks man. You saved me a LOT of time just by the little example you provided here.
most appreciated .

robcarr’s picture

The preprocess_page function generates a 'Strict' error because the drupal_render() function expects a reference as its first argument. So I've just added another line to store the value of the search form before it's then passed to the render function:

function MYTHEME_preprocess_page(&$variables){
  $search_form = drupal_get_form('search_form');
  $search_form_box = drupal_render($search_form);
  $variables['search_box'] = $search_form_box;
}
robbdavis’s picture

Thanks @arrgh this solved the strict error warning: "Strict warning: Only variables should be passed by reference..."

feo’s picture

that's a helper function

function block_render($module, $block_id) {
  $block = stratis_block_load($module, $block_id);
  $block_content = _block_render_blocks(array($block));
  $build = _block_get_renderable_array($block_content);
  $block_rendered = drupal_render($build);
  return $block_rendered;
}

...

block_render('search', 'form')
choitz’s picture

What's the benefit of preprocessing this?
I have it working using first example, but kept getting errors when trying to pre-process (as clearly I'm doing it wrong!!).

Thanks.

sammyframson’s picture

I'm not a super savvy Drupal developer and I was trying to find a way to embed the search box on a given page (e.g. my defined 404 page) but did not want to include it on all nodes of that content type - in my case the content type "page."

After digging, I was finally able to simply embed the search box on a specific page by adding the following to my page and enabling the "PHP Filter" module (p.s. you also have to enable that text format and assign permissions to that input format type at admin/config/content/formats):

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

Hope it helps someone else.

hhany’s picture

Hi, Im new here, can you help me with the alignment? by embedding the search box, is there also a way to align it horizontally with the texts in the block?
because it always moves the search box down the texts of a block whenever I embed it.

target output upon embed
ex: hello world ( search box here)

output is always:
hello world
(Search box here)

vali hutchison’s picture

Thanks @davidneedham your pre-processing example worked well for me and as you say is nice and flexible.

@choitz - when you say you got errors can you say which? One thing to check, did you rename the template_preprocess_page function with the name of your theme? If not then this would have caused errors - so replace template with the name of your theme. And this code all goes in your theme's template.php file.

Also be sure to clear drupal caches after you've added it so that Drupal adds it to it's registry.

fngatia’s picture

I tried this in Drupal 7:

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

It worked for me. Thanks to sammyframson.

vinoth.babu’s picture

Put this into your template.php file:

function YOUR_THEME_preprocess_page(&$vars) {
  $search_form = drupal_get_form('search_block_form');
  $vars['search'] = drupal_render($search_form);
}

And then you can print $search variable in your page.tpl.php file:

 print $search; 
alex.87’s picture

This is code for D8, since hasdrupal_get_form() been replaced by the form builder service.

 function mytheme_preprocess_block(&$variables) {
   $form = \Drupal::formBuilder()->getForm('Drupal\search\Form\SearchBlockForm');
   $variables['search_form'] = $form;
}