2

I want the list of the names of all regions available in the current theme in drupal 8. How to do this ?
And is it possible to get the list of the blocks placed on that region ?

6

Active ADMIN theme & active SYSTEM theme might be different.(By default active admin theme is seven & active system theme is bartik)

Get the current theme used by the below code(if you are in admin section it will return seven, else bartik)

$theme = \Drupal::theme()->getActiveTheme()->getName();

As we need the system theme, below code will do the job

$theme = \Drupal::config('system.theme')->get('default');

And pass the theme name to get all the regions

$system_region = system_region_list($theme, $show = REGIONS_ALL);

This will give you all the regions in array format

$region_list =  json_decode(json_encode(system_region_list(\Drupal::config('system.theme')->get('default'),$show = REGIONS_ALL)),true);

Second part of the question

Get the blocks in particular region . Below is the code snippet which can get the blocks placed in pre-content region:

  $blocks = \Drupal::entityManager()
      ->getStorage('block')
      ->loadByProperties(array('theme' =>  \Drupal::config('system.theme')->get('default'),'region'=>'pre_content'));
  • thank you for your attention..but this system_region_list() returning a stdClass Object, which doesn't conatin the region array/list – Jasodeep Chatterjee Sep 27 '16 at 7:19
  • have edited my answer – Shreya Shetty Sep 27 '16 at 7:23
  • My current default theme is Bartik, it has 15 regions but $region_list is listing up only 9. Even region names are also different in some cases. – Jasodeep Chatterjee Sep 27 '16 at 7:31
  • those are hidden region – Shreya Shetty Sep 27 '16 at 9:14
1

I don't know is it possible to get the blocks list in one line, but you can do it like that:

$blocks = \Drupal\block\Entity\Block::loadMultiple();
$region_name = 'content';
$block_ids = [];
foreach($blocks as $block){
  if($block->getRegion() === $region_name){
    $block_ids[] = $block->id();
  }
}
  • That has nothing to do with the question. You are listing blocks here, but he needs regions! – ssibal Sep 27 '16 at 9:08
  • It is for the question **And is it possible to get the list of the blocks placed on that region ** – Net Sep 27 '16 at 9:09
  • Sorry, you are right. I didn't read that! – ssibal Sep 27 '16 at 9:19
0

This worked for me a couple of times already, looking how the core does it:

In case of "/admin/structure/block" contains all the regions, you only have to look up how it's implemented in the core.

  1. Get the link where you saw the functionality (/admin/structure/block in our case)
  2. If you use IDE "find in path" on the core directory and paste it.
  3. You are looking for a "*.routing.yml", which will tell you which controller handles the request
  4. Now you are on your own to find it, but it's really not that hard.

As mentioned above you will need this:

system_region_list($theme, $show)

But also I suggest you to take a look at BlockListBuilder.php, in core/modules/block!

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.