When i use block_get_blocks_by_region to get the blocks assigned to a region it works well if i use the block module but when i asign the block using context the function block_get_blocks_by_region doesn't return the block.

¿Does it works as expected or i'm missing something?

Comments

Jason Dean’s picture

I'm finding exactly the same :(

dualcorpse’s picture

Hi there,

There might be some better way to do it, but I found a workaround that works like a charm in templates files :

Instead of the classic :

<?php print render(block_get_blocks_by_region('my_region_name')); ?>

With Context module, I use :

<?php if ($plugin = context_get_plugin('reaction', 'block')) : ?>
  <?php print render($plugin->block_get_blocks_by_region('my_region_name')); ?>
<?php endif; ?>

Note that if you're in a page--anything.tpl.php template, you can still use the $page variable :

<?php if ($page['my_region_name']) : ?>
  <?php print render($page['my_region_name']); ?>
<?php endif; ?>

Hope it'll help someone.

c7210’s picture

it works!thanx man you saved my day!!

tekante’s picture

Status: Active » Closed (works as designed)

As context bypasses the normal block placement mechanisms of core it is expected that the block_get_blocks_by_region function will not return blocks placed using context. Closing this as information about how to approach this with context has been provided.

peteainsworth’s picture

Thanks for this, it really helped me out. For anyone coming across this thread, to avoid a

strict warning: Only variables should be passed by reference

error, add the code to your template.php:

  function MYTHEMENAME__preprocess_node(&$vars) {
    if ($plugin = context_get_plugin('reaction', 'block')) {
      $vars['my_region_name'] = $plugin->block_get_blocks_by_region('my_region_name');
    }
  }

and then add this to your node.tpl.php:

  print render($my_region_name); 
jrogen’s picture

Issue summary: View changes

The suggestion in #5 didn't quite work for me. I found the solution here: https://www.drupal.org/node/966768

Just add the line:
drupal_static_reset('context_reaction_block_list');
below the line setting $vars and the blocks rendered correctly for me.

jimkeller’s picture

I was using this method of printing context regions in the node with success, until I had more than one context active. It seemed that the preprocess function was running multiple times, and the block content was empty on the final run (possibly because Drupal won't print the block content for the same block twice?)

I'm not entirely sure this was the best way to handle it, but it seemed to work; basically just caching the value one time and not letting block_get_blocks_by_region run over and over. However, I'm not sure how this will act with more complicated context set-ups:


$sidebar_blocks = &drupal_static(__FUNCTION__, array());

if ( $plugin = context_get_plugin('reaction', 'block') ) {
  
  if ( empty($sidebar_blocks) ) {
    $sidebar_blocks = $plugin->block_get_blocks_by_region('sidebar_left');
  }

}

$vars['regions']['sidebar_left'] = $sidebar_blocks;   

rooby’s picture

Depending on the context configuration I was still getting incorrect blocks with the above examples.
The main issue I had was the context_disable_context module was being used to disable certain contexts and that wasn't being taken into account.

This seems to work but I need to do some more thorough testing to make sure it has no negative effects.

I used this code to load all core drupal blocks combined with all context blocks and then properly render the region with correct templates.
Note that I had to invoke the context hooks. That may not always be necessary but I'm not sure yet.
This will render the sidebar_first region:

<?php
  // Get blocks from Drupal core.
  $blocks = block_get_blocks_by_region('sidebar_first');
  // Get blocks from the context module.
  module_invoke_all('context_page_condition');
  module_invoke_all('context_page_reaction');
  if ($plugin = context_get_plugin('reaction', 'block')) {
    $blocks += $plugin->block_get_blocks_by_region('sidebar_first');
    drupal_static_reset('context_reaction_block_list');
  }

  if ($blocks) {
    // If we have blocks for the region, make a region render array.
    $region = $blocks + array(
      '#theme_wrappers' => array('region'),
      '#region' => 'sidebar_first',
    );
  }
?>

You can then use drupal_render() to render that region variable.