3

I have a custom module that build a block. I'm trying to add a tpl file, but I don't get nothing. This is my code:

<?php
/**
 * Implements hook_block_info().
 *
 * This hook declares what blocks are provided by the module.
 */
function tados2_block_info() {
  $blocks = array();

  $blocks['block_with_tpl'] = array(
    // info: The name of the block.
    'info' => t('Tados2 example: block with tpl'),
    'status' => TRUE,
  );

  return $blocks;
}
/**
 * Implements hook_block_view().
 *
 * This hook generates the contents of the blocks themselves.
 */
function tados2_block_view($delta = '') {
  switch ($delta) {
    case'block_with_tpl':
      // The subject is displayed at the top of the block. Note that it
      // should be passed through t() for translation. The title configured
      // for the block using Drupal UI supercedes this one.
      $block['subject'] = t('Tados2 block with tpl');
      // The content of the block is typically generated by calling a custom
      // function.
      $block['content'] = t('Hola mundo');//_tados2_block_with_tpl_contents();
    break;
  }
  return $block;
}
?>

I'm trying to move the content to a tpl.php file.

This what I'm trying:

<?php
/**
 *
 * Implementen hook_theme
 *
 */
function tados2_theme(){
  return array(
    'tados2_block' => array(
      //'render element' => 'elements',
      'template' => 'tados2_block',
      'variables' => array('title' => NULL, 'content' => NULL),
    ),
  );
}
/**
 * preprocess function
 * 
 */ 
function template_preprocess_tados2_block(&$variables) {
  dpm($variables);
}
?>
3

You have to call your theming function:

$block['content'] = theme('tados2_block', array('title' => 'foo', 'content' => 'bar'));
  • Hello Leni,Thanks it will only for the content. For the title will be the same? – Oskar Calvo Mar 20 '12 at 13:49
3

I have tried to use a template file in my block. This is my solution.

  • Fist step: You need to register your theme. Use the following code in yourmodule.module.

    function yourmodule_theme($existing, $type, $theme, $path){
      return array(
        'yourmodule_block' => array(
          'template' => 'yourmodule',
          'variables'=> array('var1' => NULL, 'var2' => NULL),
        ),
      );
    }
    

    Note that in 'template' => 'yourmodule' 'yourmodule' is the template filename in your module folder.
    Check hook_theme() for more details.

  • Secondnd step: Use your theme for a block.

    function yourmodule_block_view($delta = '') {
      switch ($delta) {
        case 'yourmodule_block':
          $block['subject'] = 'LinhNH-Training';
          $block['content']['yourmodule'] = array(
            '#theme' => 'yourmodule_block',
            '#var1' => 'Module Title',
            '#var2'=>'Content '
          );
    
          return $block;
          break;
      }
    
      // return $block;
    }
    

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.