I have customized a block.tpl.php file and I want to access the individual fields in it. But when I put print render($content['field_name']) in my code nothing is printed out. And also print_r(array_keys($content)) doesn't give anything.

I have used the above codes in my page.tpl and node.tpl files successfully before.

I'm on D7.

So is it done somehow differently in block template? What's going on?


If you have a look at the code for the original block.tpl.php file, $content doesn't actually get run through render(), as it's already been rendered to a string.

If you want to alter the block content while it's still in a structured array you should use a hook_block_view_alter() implementation in a custom module. This is the example from the docs page:

function hook_block_view_alter(&$data, $block) {
  // Remove the contextual links on all blocks that provide them.
  if (is_array($data['content']) && isset($data['content']['#contextual_links'])) {
  // Add a theme wrapper function defined by the current module to all blocks
  // provided by the "somemodule" module.
  if (is_array($data['content']) && $block->module == 'somemodule') {
    $data['content']['#theme_wrappers'][] = 'mymodule_special_block';

How successful you'll be using this method depends on how well the modules that provide the blocks respect render arrays. If an implementation of hook_block() by a particular module flattens the block content to a string before it is made alterable, then you'll have to make the changes somewhere else in the process (probably using a theme preprocess/override).

  • 1
    I've created my block through Views. Should I follow your instructions regardless? – CobaltBabyBear May 19 '12 at 11:47
  • Blocks created with views will also go through hook_block_view_alter() so yes, this is still an option. You could also look at overriding the template file for that particular block if the block alter doesn't work – Clive May 19 '12 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.