Moving a block (view) into a different region programmatically (Drupal 7)

990 views Asked by At

I use the Commerce Kickstart Distribution of Drupal 7. I want to have the searchbar assigned to a different region on the frontpage than on all other sites in my theme. I used the approach shown here: http://saw.tl/drupal/programmatically-manage-drupal-blocks.html

function mytheme_hook_block_info_alter(&$blocks, $theme, $code_blocks) 
{
  if(drupal_is_front_page()) {
      $blocks['views]['-exp-display_products-page']['region'] = "branding";
   }
}

This is the function in my template.php. I know that the search bar is created using the views module and is not a "default block".

The name shown in the Block menu for the search bar is Exposed form: display_products-page, the module name and machine name I choosed following this tutorial http://drupalchamp.org/node/166

However, it does not work at all. I do not get any errors or warnings, the block just stays in is default region when I load the front page.

1

There are 1 answers

2
MilanG On

You can use Context module to add blocks to different regions depending on some criteria:

https://www.drupal.org/project/context

Very powerful and easy to use module so I'm advising it.

And if you prefer doing it form code you could grab block's content and print it from template directly, depending on detected page. Something like:

$block = module_invoke('views', 'block_view', 'block_machine_name');
print render($block);

Of course you would execute it conditionally...