سؤال

I'm trying to create a module that will display some last entries from database. I'd like to send last entry object to template file (guestbook-last-entries.tpl.php), that looks like that

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

I have a function that implements hook_theme

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

one that do preprocess

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

and functions that implements 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 that gets data from database

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

But in this case I get only one entry displayed. Can anyone tell me how to resolve this, how should I build that $block['content']? Thank you

هل كانت مفيدة؟

المحلول

This here wont work:

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

Maybe you want this if you need an array as the result:

// 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,
);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top