1

This is my first question on here--though I've used it for years for reference--so, please forgive me if this is trivial/already answered, but I wasn't able to find a solution.

I have a render array being returned to the 'content' attribute of a block view array. I'm just returning a table as of right now.

<?php
$data = array(
'#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
);

return $data;
?>

What I'd like to do is add additional markup at the beginning. I've tried adding a '#prefix' attribute, but the results weren't as expected; I ended up with HTML outside of the block. Anything else I thought would work seems to cause PHP errors or do nothing at all.

UnsettlingTrend
  • 452
  • 1
  • 5
  • 20

1 Answers1

3

When using HTML tags with #prefix they should be closed with #suffix. Did you remember to close the content in #prefix?

Like so:

$data = array(
    '#type' => 'markup',
    '#prefix' => '<div>',
    '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
    '#suffix' => '</div>',
);

Edit

To control the output as HTML rather than a render array one could use the hook_block_view() hook to feed the $block['content'] with pure HTML rather than a render array. According to the documentation the $block['content'] can handle both types of input.

function modulename_block_view($delta='') {
  $block = array();

  switch($delta) {
    case 'block_name' :
      $block['content'] = '<div>Content before</div>';
      $block['content'] .= theme('table', array('header' => $header, 'rows' => $rows));
      $block['content'] .= '<div>Content after</div>';
    break;
  }

  return $block;
}

If you don't want to use the block view hook I guess you could render your render arrays in this manner instead:

$render_array = array(
    '#type' => 'markup',
    '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
);

$before = '<div>Content before</div>';
$after = '<div>Content after</div>';
$data = $before.render($render_array).$after;
return $data;

See render() for a reference. Also remember to clear your caches like stated in this answer.

Community
  • 1
  • 1
Larpon
  • 812
  • 6
  • 19
  • 1
    This doesn't really answer my question. I understand what you're saying, but I was trying to use prefix for a different purpose; the purpose is what I need help with: To add more html to the block, in addition to the theme rendered table. I do appreciate the effort though. What I'd like to do is, say, add some text before or after the table in the block. – UnsettlingTrend Feb 14 '13 at 18:53
  • Ah, I see. - I edited my answer to try to reflect your refinement. – Larpon Feb 26 '13 at 14:43