Skip to content

Commit

Permalink
MDL-82359 Install: Allow install to succeed with missing default blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Jul 22, 2024
1 parent f2937c3 commit ecfdc1e
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,28 @@ public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = N
}
}

/**
* Given an array of blocks in the format used by {@see add_blocks}, removes any blocks from
* the list if they are not installed in the system.
*
* @param array $blocks Array keyed by region
* @return array Updated array
*/
public function filter_nonexistent_blocks(array $blocks): array {
$installed = array_fill_keys(
array_map(fn($block) => $block->name, $this->get_installed_blocks()), true);
$result = [];
foreach ($blocks as $region => $regionblocks) {
$result[$region] = [];
foreach ($regionblocks as $blockname) {
if (array_key_exists($blockname, $installed)) {
$result[$region][] = $blockname;
}
}
}
return $result;
}

/**
* Move a block to a new position on this page.
*
Expand Down Expand Up @@ -2684,6 +2706,9 @@ function blocks_get_default_site_course_blocks() {
/**
* Add the default blocks to a course.
*
* Because this function is used on install, we skip over any default blocks that do not exist
* so that install can complete successfully even if blocks are removed.
*
* @param object $course a course object.
*/
function blocks_add_default_course_blocks($course) {
Expand Down Expand Up @@ -2711,19 +2736,26 @@ function blocks_add_default_course_blocks($course) {
}
$page = new moodle_page();
$page->set_course($course);
$page->blocks->add_blocks($blocknames, $pagetypepattern);
$page->blocks->add_blocks($page->blocks->filter_nonexistent_blocks($blocknames),
$pagetypepattern);
}

/**
* Add the default system-context blocks. E.g. the admin tree.
*
* Because this function is used on install, we skip over any default blocks that do not exist
* so that install can complete successfully even if blocks are removed.
*/
function blocks_add_default_system_blocks() {
global $DB;

$page = new moodle_page();
$page->set_context(context_system::instance());
// We don't add blocks required by the theme, they will be auto-created.
$page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('admin_bookmarks')), 'admin-*', null, null, 2);
$page->blocks->add_blocks($page->blocks->filter_nonexistent_blocks([
BLOCK_POS_LEFT => [
'admin_bookmarks',
]]), 'admin-*', null, null, 2);

if ($defaultmypage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => 1))) {
$subpagepattern = $defaultmypage->id;
Expand All @@ -2737,22 +2769,22 @@ function blocks_add_default_system_blocks() {
$mycoursesubpagepattern = null;
}

$page->blocks->add_blocks([
$page->blocks->add_blocks($page->blocks->filter_nonexistent_blocks([
BLOCK_POS_RIGHT => [
'recentlyaccesseditems',
],
'content' => [
'timeline',
'calendar_month',
]],
]]),
'my-index',
$subpagepattern
);

$page->blocks->add_blocks([
$page->blocks->add_blocks($page->blocks->filter_nonexistent_blocks([
'content' => [
'myoverview'
]],
]]),
'my-index',
$mycoursesubpagepattern
);
Expand Down

0 comments on commit ecfdc1e

Please sign in to comment.