Drupal 7 – Creating a Module Block that Uses a Theme File

To create a Module that creates a block that makes use of a theme file in Drupal, you can use the “hook_theme()” function. Below are the steps in an example:

  1. Create a CUSTOM-TPL.tpl.php file in your module folder:
  2. In your modules .module file, use the following to call the hook theme function:

function YOURMODULE_theme() {
return array(
‘variables’ => array(‘title’ => NULL, ‘path’ => NULL), // eg: if you want $title, $path in ur tpl. you can use any variable or an entire array/object.
‘template’ => ‘CUSTOM-TPL’,

Now you can call in your hook theme function in your “hook_block_view” function:

function YOURMODULE_block_view($delta = ”) {
$block = array();
switch ($delta) {
case ‘YOURMODULE_block’:
$block[‘subject’] = ”;
$block[‘content’] = theme(‘YOURMODULE_CUSTOM_THEME_NAME’, array(‘title’ => $title, ‘path’ => $path));
return $block;

This is an expanded answer from stackexchange.com

Leave a Reply

%d bloggers like this: