Skip to content

Commit

Permalink
Add support for core dynamic_form
Browse files Browse the repository at this point in the history
  • Loading branch information
sumaiyamannan committed Jan 6, 2025
1 parent d6b9ab3 commit 28794eb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
65 changes: 42 additions & 23 deletions classes/form/editblock_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

use block_multiblock_proxy_edit_form;

use moodle_page;
use block_base;
/**
* Base class for common support when editing a multiblock subblock.
*
Expand All @@ -40,36 +42,51 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class editblock_base extends block_multiblock_proxy_edit_form {
/** @var block_base $block The block class instance that belongs to the block type being edited */
public $block;

/** @var object The page object for the page which contains the block being edited */
public $page;

/** @var object The multiblock object being edited */
public $multiblock;

/**
* Creates the instance-specific editing form.
*
* @param string|moodle_url $actionurl The form action to submit to
* @param block_base $block The block class being edited
* @param object $page The contextually appropriate $PAGE type object of the block being edited
* @param object $multiblock The multiblock object being edited (mostly for its configuration
* The block instance we are editing.
* @var block_base
*/
private $_block;
/**
* The page we are editing this block in association with.
* @var moodle_page
*/
public function __construct($actionurl, $block, $page, $multiblock = null) {
$this->block = $block->blockinstance;
$this->block->instance->visible = true;
$this->block->instance->region = $this->block->instance->defaultregion;
$this->block->instance->weight = $this->block->instance->defaultweight;
$this->page = $page;
$this->multiblock = $multiblock;
private $_page;

if (!empty($this->block->configdata)) {
$this->block->config = @unserialize(base64_decode($this->block->configdata));
/**
* Page where we are adding or editing the block
*
* To access you can also use magic property $this->page
*
* @return moodle_page
*/
protected function get_page(): moodle_page {
if (!$this->_page && !empty($this->_customdata['page'])) {
$this->_page = $this->_customdata['page'];
}
return $this->_page;
}

parent::__construct($actionurl, $this->block, $this->page);
/**
* Instance of the block that is being added or edited
*
* To access you can also use magic property $this->block
*
* If {{@see self::display_form_when_adding()}} returns true and the configuration
* form is displayed when adding block, the $this->block->id will be null.
*
* @return block_base
*/
protected function get_block(): block_base {
$this->multiblock = $this->_customdata['multiblock'];
if (!$this->_block && !empty($this->_customdata['block'])) {
$this->_block = $this->_customdata['block'];
}
return $this->_block;
}

/**
Expand All @@ -85,8 +102,10 @@ public function add_save_buttons() {

// If the page type indicates we're not on a wildcard page, we can probably* go back there.
// Note: * for some definition of probably.
if (strpos($this->multiblock->instance->pagetypepattern, '*') === false) {
$buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'));
if (isset($this->multiblock->instance->pagetypepattern)) {
if (strpos($this->multiblock->instance->pagetypepattern, '*') === false) {
$buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'));
}
}

$buttonarray[] = &$mform->createElement('cancel');
Expand Down
5 changes: 4 additions & 1 deletion classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ public static function get_edit_form($actionurl, $block, $page, $multiblock = nu
if (static::is_totara()) {
return new editblock_totara($actionurl, $block, $page, $multiblock);
} else {
return new editblock($actionurl, $block, $page, $multiblock);
$customdata['block'] = $block->blockinstance;
$customdata['page'] = $page;
$customdata['multiblock'] = $multiblock;
return new editblock($actionurl->out(false), $customdata, 'post');
}
}
}

0 comments on commit 28794eb

Please sign in to comment.