Question

I can't seem to get my custom block within my module to render the template file I created. Here is my code:

<?php

include_once 'e_most_popular.features.inc'; //this is from features module

function e_most_popular_block_info() {
   $blocks['e_most_popular'] = array(
     'info' => t('e_most_popular block TITLE'), 
     'cache' => DRUPAL_NO_CACHE, //there are a number of caching options for this
   );



  return $blocks;

}



function e_most_popular_block_view($delta = ''){

  switch($delta){

    case 'e_most_popular':

      if(user_access('access content')){ //good idea to check user perms here

         $block['subject'] = t('MYblock_TITLE');

         $block['content'] = e_most_popular_block_function_items();

      }

      break;

  }

}



function e_most_popular_block_function_items(){

  $items = array();

  $items['VAR_ONE'] = array('#markup' => 'VAR_ONE_OUTPUT'); //this is the simplest kind of render array

  $items['VAR_TWO'] = array(

                        '#prefix' => '<div>',

                        '#markup' => 'VAR_TWO_OUTPUT',

                            '#suffix' => '</div>',

                          );

//this is where the $items get sent to your default e_most_popular_block.tpl.php that gets //registered below

      return theme('e_most_popular_block_function_items', array('items' => $items)); 

    }



//here you are registering your default tpl for the above block 

function e_most_popular_theme() {   

  $module_path = drupal_get_path('module', 'e_most_popular');

  $base = array(

    'path' => "$module_path/theme",   );

     return array(

    'e_most_popular_block_function_items' => $base + array(

      'template' => 'e_most_popular_block',  

      'variables' => array('items' => NULL,),

    ),   

  ); 

}

I have confirmed that it does read the template file, since it will error if not named correctly, and I have enabled the and module assigned the block to the sidebar in the block menu. I also clear the cache after making changes. I still get no output. Here is the template file:

Template file Test
<?php 

$items = $variables['items'];
print render($items['VAR_ONE']); 

Any idea what I am doing incorrectly?

Was it helpful?

Solution

As I mentioned in my comment, the issue is that you are not returning the $block variable in your hook_block_view. That is why nothing is being output.

Check out the documentation for hook_block_view.

Your hook_block_view should be like the following:

function e_most_popular_block_view($delta = ''){
  $block = array();

  switch($delta){

    case 'e_most_popular':

      if(user_access('access content')){ //good idea to check user perms here

         $block['subject'] = t('MYblock_TITLE');

         $block['content'] = e_most_popular_block_function_items();

      }

      break;

  }
  return $block;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top