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
2021-03-08 09:20:17 +00:00
parent 96b49ddc97
commit e18b37c61d
2 changed files with 23 additions and 9 deletions
+21 -9
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;
}