2013-03-10 144 views
0

我似乎無法讓我的自定義塊內我的模塊呈現我創建的模板文件。這裏是我的代碼:Drupal 7 - 模塊塊與自定義模板不呈現

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

你是不是在你的block_view鉤退回$塊。 – mmiles 2013-03-12 13:52:52

回答

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