diff --git a/lib/adminlib.php b/lib/adminlib.php index d23d1b15885..c3ead083eaa 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -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; } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 53ca66805d3..6fdc886ab4d 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -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: