Quantcast
Channel: Lecciones Prácticas
Viewing all articles
Browse latest Browse all 33

Drupal 7: Display search box in page.tpl.php or other template files [SOLVED]

$
0
0

Let’s assume you want to display a search box in your Drupal 7 theme. You can do it several ways: rendering the search form, rendering a block which contains it, using template.php or page.tpl.php.

Thirty-second intro to Drupal theming

If you are new to Drupal, let me explain how the themes work.

Each theme is a directory (under sites/all/themes/yourthemename) with at least three files:

* The first one, the .info file, called yourthemename.info, describes the theme (regions, path to stylesheet, javascript files, blablabla).

* The second one, which contains the logic (php code) is called template.php, and

* the third one is the html markup file (ideally, with just some php print’s) called page.tpl.php.

The concept is quite simple: you generate some variables in template.php and print them in page.tpl.php.

Now that we understand the basics, you will find this examples quite self-explaining:

The dirty way: display Drupal form in page.tpl.php

First way to show a search box in your Drupal theme works as expected, but it is not the way you are supposed to implement it.

This adds the logic to show a search form directly in your View file (page.tpl.php).

This practice should be avoided, as there must not be any logic inside .tpl.php files… Anyway, Drupal allows it, so:

Edit page.tpl.php (in your theme folder, in /sites/default/MYTHEME) and add these two lines, which will render a block containing a search form:

<?php
// this is page.tpl.php
 
// insert a search_form here...
$block = module_invoke('search','block_view','search');
//  'search' is the module invoking the block. 
//  'block_view' is the operation we want to call, that is, we want to view the block, and 
//  'form' is the machine name of the block you want to print.
 
// You can get this information by hovering over the edit link on the block overview page at admin/structure/block
// Edit links will be in the form
//       /admin/structure/block/manage/MODULENAME/BLOCK_MACHINENAME/configure
 
 
// now we print the block...
print render($block);
?>

You can read more about module_invoke and render function in drupal documentation.

The right way: show a search block in your Drupal 7 theme

We can set a variable containing a search block in template.php:

<?php
// this is template.php, where the logic goes...
// replace MYTHEME with your theme name (like the theme folder containing the theme)
function MYTHEME_preprocess_page(&$variables)
{
    $block = module_invoke('search','block_view','search');
    $rendered_block = render($block);
    $variables['mysearchblock'] = $rendered_block;
}
?>

And then print that variable in your page.tpl.php file:

<?php
// this is page.tpl.php, where the presentation (view) is generated:
   // ... custom html stuff ...
 
   // print our rendered search block...
   print $mysearchblock;
?>

The right way: display Drupal 7 search form in theme

Instead of rendering the block, we will be rendering the form (not a block, but the full search form instead):

Let’s put the logic where it should be, in template.php:

<?php
// this is template.php, where the logic goes...
// replace MYTHEME with your theme name 
function MYTHEME_preprocess_page(&$variables)
{
    $search_box = drupal_render(drupal_get_form('search_form'));
    $variables['my_search_box'] = $search_box; 
}
?>

And then, in our page.tpl.php, just print that $my_search_box variable we just set earlier…

<php
// this is page.tpl.php
print $my_search_box
?>

If done like so, it will work and render the search form, but it will spit out something like
Strict warning: Only variables should be passed by reference in

This has to be with variables being passed as reference, and not creating an instance for those first. Read this for further information.

So, let’s fix it.

In template.php:

<?php
// this is template.php
 
function MYTHEME_preprocess_page(&$variables){
  // para poder usar una caja de búsqueda en los templates .tpl.php de Drupal 7...	
  $form = drupal_get_form('search_form');
  $cajabusqueda = drupal_render($form);
  $variables['cajabusqueda'] = $cajabusqueda;   
}?>

Viewing all articles
Browse latest Browse all 33

Trending Articles