Skip to content

Commit

Permalink
Merging from STABLE:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
defacer committed Jun 13, 2005
1 parent 3413766 commit b33dd23
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions blocks/moodleblock.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

/**
Expand Down
21 changes: 20 additions & 1 deletion lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b33dd23

Please sign in to comment.