1

How to create more than one block with different .tpl (theme) file in single module in Drupal 7?

Suppose I have a module name mymodule, I want to create two blocks in this module name as myblock1 and myblock2 and these two block should be associated with different tpl file like block1.tpl.php and block2.tpl.php. and both blocks have same content.

Any help?

2

Block code from custom template is here

 /**
 * Implements hook_theme().
 */
function my_custom_block_theme() {

    $theme = array();

    $theme = array(
        'my_custom_block_output' => array(
            'variables' => array('items' => array()),
            'path' => drupal_get_path('module', 'my_custom_block') . '/templates',
            'template' => 'block--my-custom-block'
        ),
        'my_custom_header_block_output' => array(
            'variables' => array('items' => array()),
            'path' => drupal_get_path('module', 'my_custom_block') . '/templates',
            'template' => 'block--my-custom-header-block'
        ),
);

    return $theme;
}

/**
 * Implements hook_block_info().
 */
function my_custom_block_block_info() {


    $blocks['my_custom_block_output_block'] = array(
        'info' => t('Custom one'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );

    $blocks['my_custom_home_block_output_block'] = array(
        'info' => t('Custom two'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );
  return $blocks;
}

**
 * Implements hook_block_view().
 */
function my_custom_block_block_view($delta = '') {
    $block = array();
    switch ($delta) {
        case 'my_custom_block_output_block':
            $block['subject'] = t('Block one');
            $block['content'] = _my_custom_block_block_content();
            break;
        case 'my_custom_home_block_output_block':
            $block['subject'] = t('Block 2');
            $block['content'] = _my_custom_home_block_block_content();
            break;
 }
    return $block;
}

/**
 * Callback implemented by hook_block_view().
 */
function _my_custom_block_block_content() {
    //Your custom values for output in $items
    $items = get_data();
 $output = theme('my_custom_block_output', array('items' => $items));
return $output;

}
function _my_custom_home_block_block_content() {
   //Your custom values for output in $items
   $items = get_data();
 $output = theme('my_custom_header_block_output', array('items' => $items));
return $output;

}

 //Data to be output
 function get_data(){
     //Your custom code
    }

Template file(.tpl) should be in "template" directory Eg :

template/block--my-custom-block.tpl.php
template/block--my-custom-header-block
  • Thanks for fast reply, but here you have used two different content for the these two block, but I have same content of both blocks, but looks will be different therefore want to use two .tpl file. – Umashankar Saw Oct 12 '15 at 10:52
  • You mean single block, but based on some condition, template will be different ? – Bipin K Oct 12 '15 at 10:54
  • Yes!, this can solve my problem, but better will be, single module, more than one blocks with different template. – Umashankar Saw Oct 12 '15 at 10:57
  • Bipin.k's answer is correct, but as per your requirement use two different .tpl files based on condition's like URL, role or some condition which should be unique in both cases to display different style's for same block. – Ajay Reddy Oct 12 '15 at 11:55
  • I edited my code, I guess there is no any other solutions than this. – Bipin K Oct 12 '15 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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