I am searching for a function that list all the blocks that are enabled in a theme. Is there any function for this purpose?
3 Answers
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.
-
Probably this is better answer, but I get Call to undefined function block_admin_display_prepare_blocks() Do I have to specify something more ? May 21, 2013 at 8:20
-
Sorry, code wasn't tested. I think it should be correct now. May 21, 2013 at 8:26
-
Actually this function does not do any thing in my custom module May 21, 2013 at 12:53
-
@Satyabratasahoo, try to put it in hook_init of your module, and at the end dpm($blocks) May 21, 2013 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. May 21, 2013 at 13:56
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);
}
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