I'm writing a module, that must override block template for some blocks. I added some settings to block configuration form, there all is ok. But i can't override block template for all blocks. I tried to override 'block' theme handler in hook_theme_registry_alter, but it does not works. I know, that i can do this by overriding block template in theme, but i want to put my template in module, and execute it for several blocks. In other words, this must be theme-independent module.
Also i tried to use hook_preprocess_block, and there i can change "theme_suggestions", but where i can set path to my template?

Comments

yuriy.babenko’s picture

Use template_preprocess_block() in your module:


function mymodule_preprocess_block(&$vars) {
  $vars['theme_hook_suggestions'][] = 'my-block';
}

Now create my-block.tpl.php in your theme's folder and clear the cache.

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

anrkaid’s picture

Thanks, but i wrote about "theme_suggestions" above. I want to place my template in module, not in theme. This is definitely possible, but i don't know how :(

WorldFallz’s picture

Did you read the reply? Its about a module not a theme... mymodule_preprocess_block not theme_preprocess_block.

anrkaid’s picture

I've read the reply. I know, that i can create template in theme folder , look:
"Now create my-block.tpl.php in your theme's folder and clear the cache.".
I need to place my template in module folder. There is a way to set template name, and i need to set template path in addition to this.

gstout’s picture

Placing the tpl.php theme file for my "module specific" block in the theme seems sloppy. But I can't get the theme system to consider tpl.php files in my module folder.

I found this http://www.phase2technology.com/node/897/

That talks about "Suggesting a Node Template" from inside a module but it doesn't seem to work for blocks.

I found this, this works, but too well. It overrides the templates FOR ALL blocks with the one you define.

http://prajwalaa.wordpress.com/2009/06/20/using-custom-template-file-in-...

Sweet Mary, This doesn't seem like a hard thing to do but I can't find a working answer.

I just want to slap the theme engine and say, " Look, unless someone overrides this tpl.php in their theme use this custom block template file here in my module."

Please help

--
Greg

gstout’s picture

Use this, replacing "gpmember_popup" with your module name. Put your tpl.php files in a theme sub-folder in your module AND make sure they would be picked up if they were in the main theme, meaning you need the right naming conventions for your blocks. Mine was called "block-gpmember_popup-0.tpl.php"

There could be horrible mistakes here but it is working for me.


/**
* Implementation of hook_registry_alter
*/
function gpmember_popup_theme_registry_alter(&$theme_registry) {

  //— Provide default node template implementations from this module,
  //— but make sure we give a corresponding tpl in a theme folder a chance
  //— to override us.
  $idx = array_search('modules/node', $theme_registry['node']['theme paths']);
  if ($idx !== False) {
    array_splice( $theme_registry['block']['theme paths'],
                  $idx+1, 0,
                  drupal_get_path('module', 'gpmember_popup') . '/theme');
  }

}

--
Greg

gstout’s picture


/**
* Implementation of hook_registry_alter
*/
function gpmember_popup_theme_registry_alter(&$theme_registry) {

  //— Provide default node template implementations from this module,
  //— but make sure we give a correspodning tpl in a theme folder a chance
  //— to override us.

  $idx = array_search('modules/system', $theme_registry['block']['theme paths']);
  if ($idx !== False) {
    array_splice( $theme_registry['block']['theme paths'],
                  $idx+1, 0,
                  drupal_get_path('module', 'gpmember_popup') . '/theme');
  }
}

--
Greg

webcomm’s picture

I would actually tend to favor the approach described in the phase2technology article...

http://www.phase2technology.com/node/897/

...rather than the solution gstout eventually came up with. The "Suggesting a Custom Template" section in the above article covers it, I'd say.

Keep in mind that your hook_block function has to return an array, as per usual, which is typically represented as the $block variable:

return $block;

But before you return $block, you can do something like...

$block['content'] = theme('my_awesome_template', $block['content']);

In the phase2 article, my_awesome_template is 'twitter_pull_listing'.

Or, if you want to pass the title and content to the template, you can do something like...

$block['content'] = theme('my_awesome_template', $block['title'], $block['content']);

In the latter case, your module's hook_theme function might look something like...

function mymodule_theme() {
  return array(
    'my_awesome_template' => array(
      'arguments' => array('title' => NULL, 'content' => NULL),
      'template' => 'my-awesome-template',
    ),
  );
}