General developer forum

add a block by default to a certain region

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
Picture of Rex Lorenzo
Re: add a block by default to a certain region
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Plugins guardiansGroup Testers

What we do is specific the block method "specialization()". You can see that this method exists in the block_base class at blocks/moodleblock.class.php.

This function is called whenever a block is instantiated. You can put some code in that funtion to set the value of "defaultregion" of that block's entry in "block_instances" to force it to be either BLOCK_POS_LEFT or BLOCK_POS_RIGHT. You can also set the defaultweight so you can force it to be on the top or bottom of either columns.

NOTE, you have access to the object from the block_instance table by using $this->instance. So you can do something like:

global $DB;
$this->instance->defaultregion = BLOCK_POS_LEFT;
$this->instance->defaultweight = 0;
$DB->update_record('block_instances', $this->instance);
Average of ratings: -