Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementing Dan Marsden's great idea for additional hooks when block

instances are deleted (bug 3446). Going a step further and adding hooks
for when block instances are created.

This probably counts as adding features to a STABLE, but I wanted to
give any future blocks that use this as much back-compat as possible.
  • Loading branch information...
commit 85ad80a03f406e90246b1d97974558e1f5bd5521 1 parent 9b1e8d7
defacer authored
Showing with 38 additions and 1 deletion.
  1. +18 −0 blocks/moodleblock.class.php
  2. +20 −1 lib/blocklib.php
View
18 blocks/moodleblock.class.php
@@ -544,6 +544,24 @@ function instance_config_commit() {
return set_field('block_instance', 'configdata', base64_encode(serialize($this->config)), 'id', $this->instance->id);
}
+ /**
+ * Do any additional initialization you may need at the time a new block instance is created
+ * @return boolean
+ * @todo finish documenting this function
+ */
+ function instance_create() {
+ return true;
+ }
+
+ /**
+ * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
+ * @return boolean
+ * @todo finish documenting this function
+ */
+ function instance_delete() {
+ return true;
+ }
+
}
/**
View
21 lib/blocklib.php
@@ -183,6 +183,18 @@ function blocks_name_allowed_in_format($name, $pageformat) {
function blocks_delete_instance($instance) {
global $CFG;
+ // Get the block object and call instance_delete() first
+ if(!$record = blocks_get_record($instance->blockid)) {
+ continue;
+ }
+ if(!$obj = block_instance($record->name, $instance)) {
+ continue;
+ }
+
+ // Return value ignored
+ $obj->instance_delete();
+
+ // Now kill the db record;
delete_records('block_instance', 'id', $instance->id);
// And now, decrement the weight of all blocks after this one
execute_sql('UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
@@ -526,7 +538,14 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid)
$newinstance->weight = empty($weight->nextfree) ? 0 : $weight->nextfree;
$newinstance->visible = 1;
$newinstance->configdata = '';
- insert_record('block_instance', $newinstance);
+ $newinstance->id = insert_record('block_instance', $newinstance);
+
+ // If the new instance was created, allow it to do additional setup
+ if($newinstance && ($obj = block_instance($block->name, $newinstance))) {
+ // Return value ignored
+ $obj->instance_create();
+ }
+
break;
}
Please sign in to comment.
Something went wrong with that request. Please try again.