Display or Print a Drupal block anywhere

posted by Ishmael Sanchez on Aug 23, 2009

During some development work I had to create a custom node template for a specific content type. The design had a menu block displayed on left sidebar on the majority of pages but the menu was also needed below the content area as a secondary navigation for this content type. I searched online and what I ended up finding were a bunch of posts about creating regions with the info file or putting a block in a region using the template.php.

These did not help because the block I wanted already existed in the left sidebar. I could've just duplicated the block but that would have been confusing to users updating the site and a waste to recreate a block that already existed.

Solution

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

To use this code for any block all you need to do is changed the module_name and the delta. Note the delta of the block can be a number or a string. Something else to note is that this only prints out the block body.

Find module name and delta info on /admin/build/block
Find module name and delta info on /admin/build/block

In Action

Displays the user 1 block
<?php $block = module_invoke('user', 'block', 'view', 1); print $block['content'];?>

Prints out the primary links block
<?php $block = module_invoke('menu', 'block', 'view', 'primary-links'); print $block['content'];?>

Resource

Tags:

Post new comment

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.

Ishmael Sanchez