function block_example_block_view_alter

Same name in other branches
  1. 3.x modules/block_example/block_example.module \block_example_block_view_alter()
  2. 4.0.x modules/block_example/block_example.module \block_example_block_view_alter()

Implements hook_block_view_alter().

This hook allows you to modify the output of any block in the system.

In addition, instead of hook_block_view_alter(), which is called for all blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a specific block. To change only our block using hook_block_view_MODULE_DELTA_alter, we would use the function: block_example_block_view_block_example_example_configurable_text_alter()

We are going to uppercase the subject (the title of the block as shown to the user) of any block if the string "uppercase" appears in the block title or subject. Default block titles are set programmatically in the subject key; titles created through the UI are saved in the title key. This module creates an example block to demonstrate this effect (default title set programmatically as subject). You can also demonstrate the effect of this hook by creating a new block whose title has the string 'uppercase' in it (set as title through the UI).

Related topics

File

block_example/block_example.module, line 237

Code

function block_example_block_view_alter(&$data, $block) {
    // We'll search for the string 'uppercase'.
    if (!empty($block->title) && stristr($block->title, 'uppercase') || !empty($data['subject']) && stristr($data['subject'], 'uppercase')) {
        // This will uppercase the default title.
        $data['subject'] = isset($data['subject']) ? drupal_strtoupper($data['subject']) : '';
        // This will uppercase a title set in the UI.
        $block->title = isset($block->title) ? drupal_strtoupper($block->title) : '';
    }
}