Exporting block positioning into code in Drupal

On most occasions, exporting configuration settings in code can be managed by the Features module, however, there are some scenarios where it can’t handle particular requirements. One particular instance, in my experience, is exporting block positioning (in relation to theme regions).

Fortunately, the block module gives us the hook_block_info_alter function that allows us to specify the region to which a certain should be placed. Since one of the function’s parameters is the $theme variable (formatted as a string), it allows us to specify block-to-region placement on a per-theme basis. An example of a block position export would be:

function hook_block_info_alter(&$blocks, $theme, $code_blocks) {
  if($theme == THEMENAME) {
    $blocks['search']['form']['region'] = 'header';
    $blocks['search']['form']['status'] = 1;
  }
}

Importance of exporting block positioning in Drupal 7

You’ll notice from code above, that I’ve used the search block as an example.

One of the theming-related changes that Drupal 7 introduces is that the rendered output of the search box is no longer available as the $search_box variable in the theme, as it once was in Drupal 6. Instead, the search form is incorporated into the block system, meaning that its position should be controlled via this mechanism.

Comments are closed.