Skip to: Site menu | Main content

Drupal Block Within a Block

I recently came across an issue where I needed to figure out how to place a block-within-a-block on a Drupal site. The client was using a custom block to pull a random view from the database using a simple PHP script in the block body. The script was a rudimentary if-then statement that generated a random number then pulled a view based on it.

My task was to integrate a block with the pool of possible random views. Turns out it is pretty simple. In the block body (be sure you're using the PHP input format), to grab a view, you use this:

print views_view_page('view_name');

To modify this to pull a block, I used this:

$block = module_invoke('module_name', 'block', 'view', 0); print $block['content'];

The module_invoke function in this case takes 4 arguments: the module's name that the block belongs to, the strings 'block' and 'view' to indicate what we'd like the function to return, and the block ID - in the example above: 0.

If you want to use a custom block, the 'module_name' is 'block'. Just be careful that you don't try to include a block in itself - sounds like an infinite loop waiting to happen...

Submitted by michael on Wed, 01/16/2008 - 10:32am
Filed under:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

block ID

Ben - how do I get the block ID if I made the view in the 'views' module?

views blocks

Thanks for the info! Note that if the block you are trying to pull content from was created in the 'views' module, the block ID will be a long alphanumeric string and will require 'quotes' around it.