1

I can change the overall layout of a block editing the block.tpl.php file. I can modify the way subject looks, but what I want to take control over contents of the block which is accessible by single line of code:

<?php print $content ?>

Now, How can I theme contents of the block, i.e. list items etc?

If I've failed to clarify myself, please mention it in your comments. Thanks :)

3 Answers 3

8

Before blocks get to the theme layer, they can be altered. Blocks are represented as "render arrays", which means you can manipulate them with the Render API.

You can use hook_block_view_alter() to change the contents of a block.

Any module or theme can implement this hook. For example, a theme named YOURTHEME would have code like this:

function YOURTHEME_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'])) {
    unset($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';
  }
}

Or, if you know in advance the module and delta you'd like to alter, you can use hook_block_view_MODULE_DELTA_alter() like this:

function YOURTHEME_block_view_MODULE_DELTA_alter(&$data, $block) {
  // This code will only run for a specific block. For example, if MODULE_DELTA
  // in the function definition above is set to "mymodule_somedelta", the code
  // will only run on the "somedelta" block provided by the "mymodule" module.

  // Change the title of the "somedelta" block provided by the "mymodule"
  // module.
  $data['subject'] = t('New title of the block');
}
2
  • Also, is it possible to customize the look & feel by editing some .tpl.php file? I'm having a menu to be displayed in the block
    – Shafiul
    Oct 30, 2011 at 3:17
  • I figured out editing modules/system/system.menus.css may solve my problem. What is the D7 way to do so? @timplunkett
    – Shafiul
    Oct 30, 2011 at 3:46
1

You can use Views to generate a block which you can exactly theme the way you want.

1
  • Also a good Idea... but can it be desirable in all cases?
    – Shafiul
    Oct 30, 2011 at 3:07
0

In short, you cannot. $content is the minimal piece of information known to the block module.

2
  • This is a broad question, like how can I style list items with CSS. You need to be more specific: What list items? What module creates them or are they user created?
    – ipwa
    Oct 29, 2011 at 18:57
  • menu modules @ipwa
    – Shafiul
    Nov 3, 2011 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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