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:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <table> <tr> <td> <th> <div> <span> <p> <br> <blockquote> <hr>
  • Lines and paragraphs break automatically.

More information about formatting options

Please wait...
He that can have patience can have what he will.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.