Drupal 7 - 模块块,自定义模板不呈现

时间:2013-03-10 18:32:22

标签: drupal-7 block drupal-themes

我似乎无法在我的模块中获取自定义块来呈现我创建的模板文件。这是我的代码:

<?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,),

    ),   

  ); 

}

我已经确认它确实读取了模板文件,因为如果没有正确命名会出错,并且我已经启用了将块分配给块菜单中侧边栏的模块。我也在进行更改后清除缓存。我仍然没有输出。这是模板文件:

Template file Test
<?php 

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

知道我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,问题是你没有在hook_block_view中返回$ block变量。这就是为什么没有输出的原因。

查看hook_block_view的文档。

您的hook_block_view应如下所示:

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;
}