MDL-84611 blocks: Respect instance_allow_multiple()

This commit is contained in:
Stefan Hanauska
2025-06-11 11:22:05 +02:00
parent 110e4f7aed
commit 172277eae4
3 changed files with 20 additions and 5 deletions
+6
View File
@@ -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.
+1
View File
@@ -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}';
+13 -5
View File
@@ -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;