2013-07-28 47 views
1

我試圖創建一個模塊,它將顯示數據庫中的最後一項。我想最後一個條目對象發送到模板文件(留言 - 最後entries.tpl.php),看起來像如何將數據發送到自定義塊內容

<p><?php render($title); ?></p> 
<?php echo $message; ?> 

我有一個實現hook_theme

function guestbook_theme() { 
    return array(
    'guestbook_last_entries' => array(
     'variables' => array(
     'entries' => NULL, 
    ), 
     'template' => 'guestbook-last-entries' 
    ), 
); 
} 

一個函數那做預處理

function template_preprocess_guestbook_last_entries(&$variables) { 
    $variables = array_merge((array) $variables['entries'], $variables); 
} 

和功能實現hook_block_view

function guestbook_block_view($delta = '') { 
    switch ($delta) { 
    case 'guestbook_last_entries': 
     $block['subject'] = t('Last entries'); 
     $block['content'] = array(); 
     $entries = guestbook_get_last_entries(variable_get('guestbook_m', 3)); 
     foreach ($entries as $entry) { 
     $block['content'] += array(
      '#theme' => 'guestbook_last_entries', 
      '#entries' => $entry, 
     ); 
     } 
     break; 
    } 
    return $block; 
} 

功能,從數據庫

function guestbook_get_last_entries($limit = 3) { 
    $result = db_select('guestbook', 'g') 
    ->fields('g') 
    ->orderBy('posted', 'DESC') 
    ->range(0, $limit) 
    ->execute(); 
    return $result->fetchAllAssoc('gid'); 
} 

但在這種情況下,我得到顯示只有一個條目中獲取數據。任何人都可以告訴我如何解決這個問題,我應該如何構建$ block ['content']? 謝謝

回答

0

在這裏,這不會工作:

$block['content'] += array(
    '#theme' => 'guestbook_last_entries', 
    '#entries' => $entry, 
); 

也許你想這個,如果你需要一個數組作爲結果:

// note that I replaced += with a simple = and added two brackets that will create a new element in that array $block['content'] 
$block['content'][] = array(
    '#theme' => 'guestbook_last_entries', 
    '#entries' => $entry, 
); 
+0

這是令人尷尬的。現在它的作品,謝謝! – jenush

相關問題