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
same question.
I got it working by....
I was able to get this working by doing the following:
That also worked for me.
That also worked for me. Thanks!
That's really cool and worked
That's really cool and worked for me from starting of Drupal 7.
Re-add the $search_box variable
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:
Don't forget to replace 'template' with the name of your theme.
Hat tip to @jenlampton for the drupal_render magic
--
David Needham
Thank you!
@davidneedham - Thanks man. You saved me a LOT of time just by the little example you provided here.
most appreciated .
Strict Error
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:Thanks @arrgh this solved the
Thanks @arrgh this solved the strict error warning: "Strict warning: Only variables should be passed by reference..."
for D7
that's a helper function
Can I ask a really Noob question?
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.
To add Search Box to a given page (drupal 7)
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):
Hope it helps someone else.
Alignment
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)
Pre Processing also worked well for me.
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_pagefunction with the name of your theme? If not then this would have caused errors - so replacetemplatewith the name of your theme. And this code all goes in your theme'stemplate.phpfile.Also be sure to clear drupal caches after you've added it so that Drupal adds it to it's registry.
I tried this in Drupal 7:
I tried this in Drupal 7:
It worked for me. Thanks to sammyframson.
Put this into your template
Put this into your template.php file:
And then you can print $search variable in your page.tpl.php file:
IN D8
This is code for D8, since has
drupal_get_form()been replaced by the form builder service.