Check active blocks on page

Check active blocks on page

על ידי Rajendra Kumar בתאריך
מספר תגובות: 1

Is there any way to list all available or assigned block on any page like landing page, course view page etc.



ממוצע דרוגים: -
בתגובה ל: Rajendra Kumar

Re: Check active blocks on page

על ידי Tim Hunt בתאריך
תמונה של Core developers תמונה של Documentation writers תמונה של Particularly helpful Moodlers תמונה של Peer reviewers תמונה של Plugin developers

For the current page, use $PAGE->blocks.

To see what is on other pages, you need code like this:

$page = new \moodle_page();
$page->set_url('/course/view.php', array('id' => $this->course->id));
$page->set_pagelayout('course');
$page->set_course($this->course);

$context = \context_course::instance($this->course->id);

$blockmanager = $page->blocks;

$blockmanager->load_blocks(true);

$missingblocks = self::$BLOCKS_TO_ENSURE_EXIST;
$desiredblockpositions = self::$BLOCK_ORDER;
$targetblockpositions = array();
foreach ($blockmanager->get_regions() as $region) {
foreach ($blockmanager->get_blocks_for_region($region) as $block) {
$instance = $block->instance;

if ($instance->parentcontextid == $context->id && in_array($instance->blockname, self::$BLOCKS_TO_REMOVE)) {
blocks_delete_instance($instance);
$this->log_action('blockremoved', $this->block_type_name($instance->blockname));
continue;
}

if (isset($missingblocks[$instance->blockname])) {
unset($missingblocks[$instance->blockname]);
} else {
if (isset($desiredblockpositions[$instance->blockname])) {
$tempname = $instance->blockname;
} else {
$tempname = 'OTHER';
}
$targetblockpositions[$instance->id] =
array($instance, $desiredblockpositions[$tempname]);
$desiredblockpositions[$tempname] += 1;
}
}
}