I'm trying to get the list of all blocks for a certain page, but I can't figure how to do that.
How do I get the list of all the blocks that are rendered for a page, independently from the region where they are put?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI'm trying to get the list of all blocks for a certain page, but I can't figure how to do that.
How do I get the list of all the blocks that are rendered for a page, independently from the region where they are put?
To get all blocks in an array by region, you can do as follow
// Get the current theme used
$theme = \Drupal::theme()->getActiveTheme()->getName();
// And pass the theme name to get all the regions
$regions = system_region_list($theme, $show = REGIONS_ALL);
// This will give you all the regions in array format
$regions = json_decode(json_encode(system_region_list(\Drupal::config('system.theme')->get('default'),$show = REGIONS_ALL)),true);
// Loop over each regions
foreach ($regions as $key => $region) {
// Get the blocks in particular region
$blocks[$key] = \Drupal::entityManager()
->getStorage('block')
->loadByProperties([
'theme' => \Drupal::config('system.theme')->get('default'),
'region'=> $key,
]);
}