4

I am searching for a function that list all the blocks that are enabled in a theme. Is there any function for this purpose?

  • Please tag your question with the Drupal version. – Beebee May 21 '13 at 8:25
  • I am currently using drupal 7.22 – Satyabrata sahoo May 21 '13 at 9:59
10

The function block_admin_display_prepare_blocks() is the function used in the block module admin page for this purpose, but it's not part of the documented API, so you cannot absolutely rely on it. You would need code along the following lines:

module_load_include('inc', 'block', 'block.admin');
global $theme_key;
$blocks = block_admin_display_prepare_blocks($theme_key);
...

This will return all blocks, unlike block_list() which only returns those for the current user and for the current page.

| improve this answer | |
  • Probably this is better answer, but I get Call to undefined function block_admin_display_prepare_blocks() Do I have to specify something more ? – Oleg Videnov May 21 '13 at 8:20
  • Sorry, code wasn't tested. I think it should be correct now. – Alfred Armstrong May 21 '13 at 8:26
  • Actually this function does not do any thing in my custom module – Satyabrata sahoo May 21 '13 at 12:53
  • @Satyabratasahoo, try to put it in hook_init of your module, and at the end dpm($blocks) – Oleg Videnov May 21 '13 at 13:00
  • It's not going to do anything on its own. It depends on how you want to process the data returned. The dpm() function mentioned above will help you to understand the data returned. You will need to install the Devel module first for dpm to be available. Once you understand the structure of the data, it's up to you to present it using PHP. If you need help with that, please add to your question. – Alfred Armstrong May 21 '13 at 13:56
1

You try with the following code.

global $theme;
$all_regions = system_region_list($theme);
$blocks = array();

foreach (array_keys($all_regions) as $region) {
   $blocks += block_list($region);
}
| improve this answer | |
0

I used Alfred Armstrong's code to write a quick and dirty test function:

  function is_block_visible($block_name) {
    module_load_include('inc', 'block', 'block.admin');
    global $theme_key;
    $blocks = block_admin_display_prepare_blocks($theme_key);
    foreach ($blocks as $block) {
      if ($block['delta'] == $block_name && $block['region'] != -1) {
        sdpm("The block '" . $block['delta'] . "' is visible in the '" . $block['region'] . "' region.");
      }
    }
  }

What I would like to do, but can't get to give me anything but WSOD or a zero length array is to use the block_list() function which I understand returns a list of visible blocks on the current page for the current user. Any help to get that to work would be greatly appreciated. Nat

| improve this answer | |

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.