Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 370 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to insert a block into a node or template in Drupal 7?

#1
In Drupal 6, it was easy to insert a block into a template with the following code:

$block = module_invoke('views', 'block', 'view', 'block_name');
print $block['content'];

However, using the same instructions in Drupal 7 does not seem to work. I have looked around and cannot find the new method.

Does Drupal 7 have a routine that can allow for programmatically inserting a block into a template or node?
Reply

#2
Have a look how Drupal does it in [_block_render_blocks][1]. The result of that function gets passed to [drupal_render][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
This appears to be the solution for inserting blocks into templates for Drupal 7, but it seems a bit clunky and I have no idea about impact on performance:

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

If anyone has a better procedure, please do add.
Reply

#4
Just tested this in drupal 7 and it works:

$bloqueServicios = module_invoke('views', 'block_view', 'servicios-blo_home');
print render($bloqueServicios);

Good luck!
Reply

#5
The `module_invoke()` function works. However, I found that rendering a block this way apparently won't use a custom template for that block. This might be OK depending upon your needs.

As commented before in other answers, this works as well and also makes use of custom templates:

$raw_block = block_load('your-module', 'delta');
$rendered_block = drupal_render(_block_get_renderable_array(_block_render_blocks(array($raw_block))));
print $rendered_block;

So, if you have a custom `block--your-module--delta.tpl.php` template file, it will be used to format the block.

Source:

[To see links please register here]

Reply

#6
In my search to include a block in a template, i came across this post.

As an addition, if you want to include a custom block (that you added through the block interface) you have to use (instead of `block_load();` in drupal 7)

$block = block_get_custom_block($bid);
$content = $block['body'];

Reply

#7
This work for me:

98 is the id of the block

$block =block_load('block',98);
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
Reply

#8
With wrburgess's answer you may get an error if your server is using a newer version of PHP.

Strict warning: Only variables should be passed by reference in include()...

This is what I did to not cause/get rid of the error.

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

#9
$block = module_invoke('menu_block', 'block_view', '6');
echo render ($block['content']);

This works for me for printing menu block.


Reply

#10
**D7:**

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

**'module_name'** = The machine name of the module (i.e. the module's folder name). This is true for core modules too, so for instance 'search', 'user' and 'comment' would all work here.

**'block_delta'** = The machine name of the block. You can determine what this is by visiting the block administration page and editing the block. The URL for editing a webform block, for instance, would be something like:

Drupal 7: admin/structure/block/manage/**webform**/**client-block-11**/configure

In this example, **'webform'** is the module's name, **'client-block-11'** is the block's delta.

Custom blocks will have module name of 'block' and a number for a delta, which you can also find by editing the block.


More information: [

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through