0

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

<?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']); 

知道我做错了什么吗?

4

1 回答 1

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;
}
于 2013-03-12T21:12:19.420 回答