0

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?

2

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,
    ]);
}
  • how to get blocks according to current page in drupal 8? – Amit May 8 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.