I have a template of my custom function and I am display certain number of items per row in this template.

For this reason I need to figure out if there are any blocks on the current page in any of the sidebars of my theme(left and right) so I can adjust the number of visible items.

So my question is how to get the list of blocks that are displayed on the current route in certain region and their visibility conditions were already processed?

I do not need to have exact information, ie. if some blocks have #access set to false or if they return empty render arrays are not important.

1 Answer 1


There's a block.repository service with a getVisibleBlocksPerRegion() method. You can either use that or look at what it's doing.

This will cover access checks as well but not empty blocks.

This is however quite a bit of an overhead as all blocks are loaded and access checked twice. There is an issue to add caching there.

Alternatively, you can use preprocess of the region templates I guess.

  • "Alternatively, you can use preprocess of the region templates I guess." - not really since I cannot provide template variable based on content of another template. That method looks fine to me, the blocks are loaded as entities so they are in static cache already. Not much duplicate processing is going on as I see(only access/ conditions). Thaks.
    – user21641
    Commented Nov 8, 2015 at 12:30
  • Yes, and access is the part that can be very slow. For example if you have many content blocks. See drupal.org/node/2479459
    – Berdir
    Commented Nov 8, 2015 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.