1

データベースの最後のエントリを表示するモジュールを作成しようとしています。最後のエントリ オブジェクトをテンプレート ファイル (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');
}

ただし、この場合、表示されるエントリは 1 つだけです。これを解決する方法を教えてもらえますか? $block['content'] をビルドするにはどうすればよいですか? ありがとうございました

4

1 に答える 1