Home
Membership
Products
Support
Blog
Login
Drupal 7 - Printing block region in node.tpl.php
Submitted 2 years 5 months ago by
duckz
.
26 comments
User Comments
jive says :
I was searching for this very thing. Thanks! works like a charm!
Renovations Adelaide says :
Thanks for publishing these details within your site.
Anonymous says :
Excellent! Just what I was looking for :)
Nubimmorybymn says :
Wow!, this was a real quality post. In theory I'd like to write like this too - taking time and real effort to make a good article on Kosttillskott... but what can I say... I keep putting it off and never seem to get something done.
Rolainfalay says :
Nice blog here! Also your website loads up fast! What host are you using? I wish my website (Papper) loaded up as fast as yours lol
aquariumtap says :
If you want those blocks to be available to hook_preprocess_region, add a bit more information:
$variables['banner_space'] = $blocks;
$variables['banner_space']['#theme_wrappers'] = array('region');
$variables['banner_space']['#region'] = 'your_block_region_machine_name';
duckz says :
Thanks for providing the code, I was assuming people will create the regions from the theme.info files since they are capable to edit the template.php files.
But your code will provide user with more options that they can actually create a block region without the need to edit .info file.
aiphes says :
hi
how to do to have multiple regions in D7 , i've posted here : http://drupal.org/node/1232500
if someone could take a look to ..
thanks
aiphes says :
hi
how to do to have multiple regions in D7 , i've posted here : http://drupal.org/node/1232500
if someone could take a look to ..
thanks
duckz says :
edit the theme .info to register your multiple custom block regions, then print each of the regions in the node.tpl (or other place) by invoking :
print render(block_get_blocks_by_region('your_block_region_machine_name'));for each block region that you want to print
aiphes says :
ok it works but no need to add code in template.php ? your ways works but how can do this code conditionnaly rendered by php if region isnt empty ?
thanks
duckz says :
Adding this code in template.php
$variables['foo'] = block_get_blocks_by_region('your_block_region_machine_name');and when rendering it in tpls (eg node.tpl)
print render($foo);is optional and not required.
if you want to conditionally rendering you'll need to split the function :
$block_region = block_get_blocks_by_region('your_block_region_machine_name');
if (isset($block_region)) { // you'll need to double check the actual return value for the $block_region, this is just an example and it may not work
print render($block_region);
}
BadDaddy says :
Awesome, exactly what I needed! Thanks
Jennifer says :
Could you be so kind to show me how to put 2 or 3 blocks in bottom 1 region in Drupal 7 theme.
Brent says :
I've been searching this code many times but I didn't found it,thanks I found this sites
Printing
nmeegama says :
All though this method works fine for rendering regions i found a bug in this method, Process to recreate the BUG:
Create a view block
Add a exposed filter to the block
Change the Ajax settings of the view block to ON
Assign the block to a region (ex: region_test)
Now render the region using the above method after adding the code to the teplate.php file
The block filtering will not work (a js error appears) Does anyone know the reason for this , If so please reply
duckz says :
My best guess is you will need to include views js (or other required js) manually
wismbuh says :
how to set on spesific location?
i want to insert a custom region between "content" and "comment" in D7.
duckz says :
you can do that in node.tpl.php or comment-wrapper.tpl.php or use Display suite and add a preprocess block field.
dating sites says :
ok it works but no need to add code in template.php ? your ways works but how can do this code conditionnaly rendered by php if region isnt empty ?
duckz says :
If Region is empty then the function will return empty value thus you can check conditionaly by checking if the variable return value is empty or not.
Special says :
Perfect. I've been searching for at least 20 minutes. Solves my area rendering problem.
Thanks
Josh says :
I've been using this on my site, but I've run into a problem. Calling block_get_blocks_by_region() causes the render array to be created a second time — functionally causing the code behind a block to execute twice. Particularly if the block queries the database, this adds a significant performance issue. Have you run into this? Any way around it?
duckz says :
The other way around is probably by calling a single block rather than calling all block in a region.
Probably by tracking down Drupal function starting from block_get_blocks_region() and grab the portion where Drupal is actually retrieving a single block and use it instead of block_get_blocks_by_region()
Josh says :
I figured out another way to do it. It adds a global variable to your template.php (not the best), but it avoids the double (and sometimes triple) block execution that causes the performance drain.
In template.php:
and then:
Finally, hide the regions in your node.tpl.php, and then render and print them where you want them.
Is there a better way to code this that uses the same underlying principle?
duckz says :
AFAIK you don't need to use global variable to move the region array from page to node.
Just inject the variable into node object found in preprocess_page and it will show in the preprocesss_node
// Sample array structure for node object found in the preprocess_page array( 'page' => array( 'content' => array( // This will be region name 'system_main' => array( 'nodes' => array( '380' => array( // this will be node nid '#node' // this is the node object, inject code here .............................but it will need debug and implementation for 100% sure.
Post New comment