Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
Jason Dean CreditAttribution: Jason Dean commentedI'm finding exactly the same :(
Comment #2
dualcorpse CreditAttribution: dualcorpse commentedHi 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 :
With Context module, I use :
Note that if you're in a page--anything.tpl.php template, you can still use the $page variable :
Hope it'll help someone.
Comment #3
c7210 CreditAttribution: c7210 commentedit works!thanx man you saved my day!!
Comment #4
tekante CreditAttribution: tekante commentedAs 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.
Comment #5
peteainsworth CreditAttribution: peteainsworth commentedThanks for this, it really helped me out. For anyone coming across this thread, to avoid a
error, add the code to your template.php:
and then add this to your node.tpl.php:
Comment #6
jrogen CreditAttribution: jrogen commentedThe 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.
Comment #7
jimkeller CreditAttribution: jimkeller commentedI 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:
Comment #8
rooby CreditAttribution: rooby commentedDepending 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:
You can then use drupal_render() to render that region variable.