How do I dynamically modify a block's mark up at render time, in Drupal 7? I have a page with the following structure:


Header zone


Header One Area (block)

Content zone


Content Area (node)



The rendered node has an image attached to it. What I need to have happen is to make the attached image render inside the "Header One Area" block instead of as part of the content.

This is proving difficult, since blocks and their #markup are rendered before a node is processed. The better idea I had is to create a preprocess function for the page, through code figure out the node_id being rendered, grab its images, and then dynamically modify the blocks markup.

I have tried creating a MYTHEME_preprocess_page() method inside my template.php and then accessing the block via:

if ($block = block_get_blocks_by_region('header_one_area')) {
   // what code do I need here to make a #markup modification stick ???
   $block['block_4']['#markup'] = "&lt;h2&gt;TEST&lt;/h2&gt;";
   print kpr($block);
}


I'm not quite sure how to actually modify its markup dynamically before it is rendered here.

Do you have any suggestion?
I think you are looking for hook_block_view_alter(). It provides both the alterable $data, as well as $block, which contains the module and delta.

To determine which blocks are available, you could start off with:

function MYTHEME_block_view_alter(&amp;$data, $block) {
  kpr($block-&gt;module);
  kpr($block-&gt;delta);
}


Then once you find the module:

/**
 * Implements hook_block_view_alter().
 */
function YOURTHEME_block_view_alter(&amp;$data, $block) {
  if ($block-&gt;module == 'block' &amp;&amp; $block-&gt;delta == 1) {
    $data['content'] = '&lt;p&gt;New content&lt;/p&gt;';
  }
}


Or even:

/**
 * Implements hook_block_view_MODULE_DELTA_alter().
 */
function YOURTHEME_block_view_block_4_alter(&amp;$data, $block) {
  $data['content'] = '&lt;p&gt;New content&lt;/p&gt;';
}