1

I know I can just override block.tpl.php using the normal template system. But is there a way to set the tpl file when I define a block in my module ?

I thinking of an equivalent to this:

function theming_example_theme() {
  return array(
    'theming_example_text_form'  => array(
      'render element' => 'form',
      // In this one the rendering will be done by a tpl.php file instead of
      // being rendered by a function, so we specify a template.
      'template' => 'theming_example_text_form',
    ),
  );
}

1 Answer 1

3

Most of the times I just do

$block['subject'] = t('My block title');
$block['content'] = theme('my_custom_theme', $block_content);

return $block;

from hook_block_view() function if you're making a module that creates a block. The theme function my_custom_theme can use a template file such as my-custom-theme-block.tpl.php. Also you can suggest theme hooks through hook_preprocess_block. Just check for the delta of your own block and set a theme hook there.

function HOOK_preprocess_block(&$vars) {
  if ($vars['block']->delta == 'my-block-delta') {
    $vars['template_suggestions'][] = 'my-custom-theme-block';
  }
}
5
  • Additionally, choosing which template file to use is the theme layer's job. If you hard-code it in your block definition function, no one will be able to override it in their theme later, whereas if you define it as a custom theme function, the active theme can override it. Sep 5, 2012 at 19:40
  • @theunraveler If the theme function hardcoded into HOOK_block_view defines a template file, it should be overridable if you just create a same name file in your theme directory. template_suggestions also work the same way. Drupal will know that my-custom-theme-block.tpl.php in your theme directory will override my-custom-theme-block.tpl.php in the module directory.
    – Beebee
    Sep 6, 2012 at 9:51
  • Right...I was supporting your answer and giving a further reason why yours is the best practice. Sorry that wasn't clearer :-) Sep 6, 2012 at 14:38
  • I guess we both misunderstood each other :) Thanks for the addition. Much appreciated.
    – Beebee
    Sep 7, 2012 at 9:17
  • So is this preferable to using e.g. block--themename--delta.tpl.php and not implementing book_block_view() at all?
    – Sam Wilson
    Dec 9, 2014 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.