From 172277eae4e7d67e0a840516f81049c0f1ddfc60 Mon Sep 17 00:00:00 2001 From: Stefan Hanauska Date: Wed, 11 Jun 2025 11:17:43 +0200 Subject: [PATCH] MDL-84611 blocks: Respect instance_allow_multiple() --- blocks/tests/externallib_test.php | 6 ++++++ lang/en/error.php | 1 + lib/blocklib.php | 18 +++++++++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/blocks/tests/externallib_test.php b/blocks/tests/externallib_test.php index 59fda684872..32fa4f15a13 100644 --- a/blocks/tests/externallib_test.php +++ b/blocks/tests/externallib_test.php @@ -44,6 +44,7 @@ final class externallib_test extends externallib_advanced_testcase { global $DB, $FULLME; $this->resetAfterTest(true); + $this->setAdminUser(); $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); @@ -77,6 +78,7 @@ final class externallib_test extends externallib_advanced_testcase { global $DB, $FULLME; $this->resetAfterTest(true); + $this->setAdminUser(); $user = $this->getDataGenerator()->create_user(); @@ -141,6 +143,7 @@ final class externallib_test extends externallib_advanced_testcase { global $DB, $FULLME; $this->resetAfterTest(true); + $this->setAdminUser(); $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); @@ -237,6 +240,7 @@ final class externallib_test extends externallib_advanced_testcase { global $DB, $CFG; $this->resetAfterTest(true); + $this->setAdminUser(); // Enable MathJax filter in content and headings. $this->configure_filters([ @@ -362,6 +366,7 @@ final class externallib_test extends externallib_advanced_testcase { public function test_get_dashboard_blocks_default_dashboard_including_sticky_block(): void { global $PAGE, $DB; $this->resetAfterTest(true); + $this->setAdminUser(); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. @@ -410,6 +415,7 @@ final class externallib_test extends externallib_advanced_testcase { public function test_get_dashboard_blocks_custom_user_dashboard(): void { global $PAGE, $DB; $this->resetAfterTest(true); + $this->setAdminUser(); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. diff --git a/lang/en/error.php b/lang/en/error.php index 8af098aea4c..4ead96ac6d4 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -31,6 +31,7 @@ $string['agelocationverificationdisabled'] = 'Age and location verification disa $string['authnotexisting'] = 'The autorization plugin doesn\'t exist'; $string['backupcontainexternal'] = 'This backup file contains external Moodle Network Hosts that are not configured locally'; $string['backuptablefail'] = 'Backup tables could NOT be set up successfully!'; +$string['blockcannotadd'] = 'Cannot add block'; $string['blockcannotconfig'] = 'This block does not support global configuration'; $string['blockcannotinistantiate'] = 'Problem in instantiating block object'; $string['blockcannotread'] = 'Could not read data for blockid= {$a}'; diff --git a/lib/blocklib.php b/lib/blocklib.php index 865efe68f05..7dc4ef80a3c 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -245,18 +245,18 @@ class block_manager { * @return boolean - is there one of these blocks in the current page? */ public function is_block_present($blockname) { - if (empty($this->blockinstances)) { + if (empty($this->birecordsbyregion)) { return false; } $requiredbythemeblocks = $this->get_required_by_theme_block_types(); - foreach ($this->blockinstances as $region) { + foreach ($this->birecordsbyregion as $region) { foreach ($region as $instance) { - if (empty($instance->instance->blockname)) { + if (empty($instance->blockname)) { continue; } - if ($instance->instance->blockname == $blockname) { - if ($instance->instance->requiredbytheme) { + if ($instance->blockname == $blockname) { + if ($instance->requiredbytheme) { if (!in_array($blockname, $requiredbythemeblocks)) { continue; } @@ -834,6 +834,14 @@ class block_manager { $pagetypepattern = $this->page->pagetype; } + if (!empty($this->birecordsbyregion)) { + $addableblocks = $this->get_addable_blocks(); + + if (!array_key_exists($blockname, $addableblocks)) { + throw new moodle_exception('blockcannotadd'); + } + } + $blockinstance = new stdClass; $blockinstance->blockname = $blockname; $blockinstance->parentcontextid = $this->page->context->id;