Using the block manager class

Using the block manager class

by Mike Grant -
Number of replies: 2

Hi,

I'm currently working on a block for the university I work at, where we're trying to prompt our users to create courses to a basic standard, and one of the things that we would like to do is suggest that users add certain blocks to the page.

I'm getting a little unstuck when trying to query if certain blocks exist 

$block_manager = new block_manager($PAGE);
$block_manager->load_blocks(true);
if($block_manager->is_block_present('name_of_block')) {
    echo "Found";
} else {
    echo "Not Found";

}

It seems that I'm querying an empty protected variable in the class, but I can't seem to figure out how to populate that variable.

Any help would be great!

Average of ratings: -
In reply to Mike Grant

Re: Using the block manager class

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You should change the first line to

$block_manager = $PAGE->blocks;

Also, if you are in a block of your own, you should use $this->page, instead of global $PAGE.

Not sure if those two changes are enough to make it work.

In reply to Tim Hunt

Re: Using the block manager class

by Mike Grant -

Thank you very much for that, just in case anyone else is searching for the same thing, this is the code i'm using to detect if blocks are added to a page.

Thanks for your help Tim!

$block_manager = $PAGE->blocks;
$block_manager->load_blocks(true);
if($block_manager->is_block_present('block_name')) {
echo "Found";
} else {
echo "Not Found";
}