我正在尝试创建一个显示数据库中最后一个条目的模块。我想将最后一个条目对象发送到模板文件(guestbook-last-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 :(得分: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,
);