MDL-45242 Admin: Added lazy-loading callback to multicheckbox

Currently admin_setting_configselect has lazy-loading support via a
callback function (so you don't have to make pointless single-use
classes for each unusual setting), but this is not present in other
similar types.

This commit adds identical support to
admin_setting_configmulticheckbox.
This commit is contained in:
sam marshall 2020-09-30 14:05:43 +01:00
parent 96b49ddc97
commit e18b37c61d
2 changed files with 23 additions and 9 deletions

View File

@ -3066,34 +3066,46 @@ class admin_setting_configcheckbox extends admin_setting {
class admin_setting_configmulticheckbox extends admin_setting {
/** @var array Array of choices value=>label */
public $choices;
/** @var callable|null Loader function for choices */
protected $choiceloader = null;
/**
* Constructor: uses parent::__construct
*
* The $choices parameter may be either an array of $value => $label format,
* e.g. [1 => get_string('yes')], or a callback function which takes no parameters and
* returns an array in that format.
*
* @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
* @param string $visiblename localised
* @param string $description long localised info
* @param array $defaultsetting array of selected
* @param array $choices array of $value=>$label for each checkbox
* @param array|callable $choices array of $value => $label for each checkbox, or a callback
*/
public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
$this->choices = $choices;
if (is_array($choices)) {
$this->choices = $choices;
}
if (is_callable($choices)) {
$this->choiceloader = $choices;
}
parent::__construct($name, $visiblename, $description, $defaultsetting);
}
/**
* This public function may be used in ancestors for lazy loading of choices
* This function may be used in ancestors for lazy loading of choices
*
* Override this method if loading of choices is expensive, such
* as when it requires multiple db requests.
*
* @todo Check if this function is still required content commented out only returns true
* @return bool true if loaded, false if error
*/
public function load_choices() {
/*
if (is_array($this->choices)) {
return true;
if ($this->choiceloader) {
if (!is_array($this->choices)) {
$this->choices = call_user_func($this->choiceloader);
}
}
.... load choices here
*/
return true;
}

View File

@ -33,6 +33,8 @@ information provided here is intended especially for developers.
* New DML driver method `$DB->sql_group_concat` for performing group concatenation of a field within a SQL query
* Added new class, AMD modules and WS that allow displaying forms in modal popups or load and submit in AJAX requests.
See https://docs.moodle.org/dev/Modal_and_AJAX_forms for more details.
* Admin setting admin_setting_configmulticheckbox now supports lazy-loading the options list by
supplying a callback function instead of an array of options.
=== 3.10 ===
* PHPUnit has been upgraded to 8.5. That comes with a few changes: