Would appreciate it if someone could let me know how to print a block in node.tpl.php. I basically have a block created through a view and I haven't set a region for that block as I would like to do is display this block in a specific position in all my node pages. I tried rendering the block in my custom theme's template file in the mytheme_preprocess_node function and then printing it in my node.tpl.php file. However, that is not working for me. Is it too late to render a block in the preprocess_node function? How do we basically set up the block so that we can access it in the node.tpl.php file?

Any help would be appreciated....Thanks.

Comments

VM’s picture

samspinoy’s picture

Check out the Display Suite module. It allows you to add the block as a field in the "Manage display" tab of your content type. You can then place it wherever you want on your node page.

shabana.navas’s picture

@samspinoy Thanks for the suggestion, but unfortunately, I am restricted to the resources at hand and do not want to install another module to get this block to display.

Shabana Navas
shabana@cheekymonkeymedia.ca
Developer
Cheeky Monkey Media
cheekymonkeymedia.ca
1-800-818-4564
Skype: super.shaba

shabana.navas’s picture

This is what I did in my theme's template file:

<?php
function mytheme_preprocess_node(&$vars) {
  $vars['my_block'] = mytheme_block_render('views', 'myblock-block_1');
}
?>

<?php
function mytheme_block_render($module, $block_id) {
  $block = 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;
}
?>

And in my node.tpl.php file, I just do:

<?php
 print $my_block;
?>

However, this is giving me a memory limit exhausted error. I can get it to work perfectly if I render the block in mytheme_preprocess_page function and print it in page.tpl.php, but I need to print this in node.tpl.php. I tried the following as well, just like it's recommended in the link that you provided:

<?php
function mytheme_block_render($module, $block_id) {
 $block = module_invoke('views', 'block_view', 'myblock-block_1');
 return render($block);
}
?>

That is giving me the same memory exhausted error. Is it too later to render a block in the node preprocess function? Why can I render it in the page preprocess function but not in node? Is there some other preprocess function that we can render it in and then have access to it in node.tpl.php?

Shabana Navas
shabana@cheekymonkeymedia.ca
Developer
Cheeky Monkey Media
cheekymonkeymedia.ca
1-800-818-4564
Skype: super.shaba

imranweb’s picture

<?php
$block = block_load('views', 'block_name');     
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));       
print $output;
?>

Try it, should work.

-Imran

shabana.navas’s picture

Actually tried that as well Imran, but it was giving me the memory exhausted error again. I got the solution (my post) finally for it! I just needed to add the block using module_invoke directly in the node.tpl.php file, instead of adding it in the template's preprocess_node function. So, this is how I got it working:

<?php
 $my_block = module_invoke('views', 'block_view', 'myblock-block_1');
  print render($my_block['subject']);
  print render($my_block['content']);
?>

Thanks for spending the time answering back. Really appreciate it.

Shabana Navas
shabana@cheekymonkeymedia.ca
Developer
Cheeky Monkey Media
cheekymonkeymedia.ca
1-800-818-4564
Skype: super.shaba

shabana.navas’s picture

This is embarrassing, but Imran, you are right, the code you posted does work if we just paste it directly in the node.tpl.php file. Dumb me, was adding it in the preprocess node, and that is what was messing things up. So the following works in node.tpl.php:

<?php
$block = block_load('views', 'block_name');    
print render(_block_get_renderable_array(_block_render_blocks(array($block))));      
?>

Once again, thanks Imran!!

Shabana Navas
shabana@cheekymonkeymedia.ca
Developer
Cheeky Monkey Media
cheekymonkeymedia.ca
1-800-818-4564
Skype: super.shaba

chegor’s picture

this code also works in block.tpl.php

thank you all!

cabplan’s picture

Hi, so I am trying to print out a Menu block in a custom node tpl but nothing is printing out, if I use echo "hello world" it does print that, I tried this code:

<?php
$my_block = module_invoke('views', 'block_view', 'sub-nav');
  print render($my_block['subject']);
  print render($my_block['content']);
echo "hello world";
?>

<?php
$block = block_load('views', 'sub-nav');
print render(_block_get_renderable_array(_block_render_blocks(array($block))));
?>

But neither actual print the menu, again this is a Menu Block, would this code apply to that type of module as well?

Terebinth’s picture

Hi, could someone please point out how this would work for blocks not done through views. I'm trying to display the whois-block from the Whois Lookup module.
Thanks!!

shabana.navas’s picture

This will work on all blocks regardless of which module it comes from. You just need to change the module name where it says 'views':

<?php
$block = block_load('views', 'block_name');
print render(_block_get_renderable_array(_block_render_blocks(array($block))));
?>

You can actually get the accurate naming for your particular block from the block configure link or from the block table.

Shabana Navas
shabana@cheekymonkeymedia.ca
Developer
Cheeky Monkey Media
cheekymonkeymedia.ca
1-800-818-4564
Skype: super.shaba

Tharick’s picture

Hi @shabana, really it works well, you saved my time.

ajeya1988’s picture

Thanks shabana ...above code works fine for me too

benjamin_dk’s picture

Hi Shabana,
your embedding code works, but I get a "strict warning"

Strict warning: Only variables should be passed by reference

This is probably because of me using a newer version of php. (PHP 5.5.9-1ubuntu4.5).

This, however, worked for me without errors

$blockObject = block_load('views', 'block_name');
$block = _block_get_renderable_array(_block_render_blocks(array($blockObject)));
$output = drupal_render($block);
print $output;

found here http://stackoverflow.com/questions/4731420/how-to-insert-a-block-into-a-...

TLGwegg’s picture

Hmm It is not Printing, or giving me an error. Maybe I am doing something wron with $blockObject?
How can I check?

Edit:
This is not the same, I don't think. But it worked for what I needed.
<?php print views_embed_view($name, $display_id = 'default'); ?>
The code I used in my site is below:
<?php print views_embed_view('retailers_shops', $display_id = 'default'); ?>
If some one can explain how the function that I have and the one before are different, that would be great.
:D Thanks

Shashwat Purav’s picture

With reference to link shared by benjamin_dk.

The reason we cannot render render_block_get_renderable_array(_block_render_blocks(array($blockObject))) is that drupal_render() is defined as drupal_render(&$elements).

As explained on Passing by Reference, when a function needs a reference as parameter, we can pass to the function:

Variables
The result of new
References returned from functions

We can only use a variable to store the result of function to render, and pass that variable to render() or drupal_render().

Following trick worked for me:

$blockObject = block_load('views', 'block_name');
$block_render_blocks = _block_render_blocks(array($blockObject))
$block_get_renderable_array = _block_get_renderable_array($block_render_blocks);
$output = drupal_render($block_get_renderable_array);
print $output;

Skype ID: shashwatpurav8

keti-1931’s picture

thanks shabana

keti-1931’s picture

thanks.