MDL-82481 customfield_select: return field options pre-formatted.

This commit is contained in:
Paul Holden
2024-07-18 14:03:18 +01:00
parent 904222b6f2
commit 44801c448c
4 changed files with 44 additions and 14 deletions
@@ -68,15 +68,9 @@ class data_controller extends \core_customfield\data_controller {
$field = $this->get_field();
$config = $field->get('configdata');
$options = $field->get_options();
$formattedoptions = array();
$context = $this->get_field()->get_handler()->get_configuration_context();
foreach ($options as $key => $option) {
// Multilang formatting with filters.
$formattedoptions[$key] = format_string($option, true, ['context' => $context]);
}
$elementname = $this->get_form_element_name();
$mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $formattedoptions);
$mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $options);
if (($defaultkey = array_search($config['defaultvalue'], $options)) !== false) {
$mform->setDefault($elementname, $defaultkey);
@@ -119,8 +113,7 @@ class data_controller extends \core_customfield\data_controller {
$options = $this->get_field()->get_options();
if (array_key_exists($value, $options)) {
return format_string($options[$value], true,
['context' => $this->get_field()->get_handler()->get_configuration_context()]);
return $options[$value];
}
return null;
@@ -62,7 +62,11 @@ class field_controller extends \core_customfield\field_controller {
public function get_options(): array {
$optionconfig = $this->get_configdata_property('options');
if ($optionconfig) {
$options = preg_split("/\s*\n\s*/", trim($optionconfig));
$context = $this->get_handler()->get_configuration_context();
$options = array_map(
fn(string $option) => format_string($option, true, ['context' => $context]),
preg_split("/\s*\n\s*/", trim($optionconfig), -1, PREG_SPLIT_NO_EMPTY),
);
} else {
$options = array();
}
@@ -111,7 +115,7 @@ class field_controller extends \core_customfield\field_controller {
$ret = [];
foreach ($values as $value) {
if (isset($options[$value])) {
$ret[$value] = format_string($options[$value]);
$ret[$value] = $options[$value];
}
}
$ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
+33 -3
View File
@@ -18,15 +18,18 @@ namespace customfield_select;
use core_customfield_generator;
use core_customfield_test_instance_form;
use stdClass;
/**
* Functional test for customfield_select
*
* @package customfield_select
* @covers \customfield_select\data_controller
* @covers \customfield_select\field_controller
* @copyright 2019 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_test extends \advanced_testcase {
final class plugin_test extends \advanced_testcase {
/** @var stdClass[] */
private $courses = [];
@@ -150,12 +153,40 @@ class plugin_test extends \advanced_testcase {
$this->assertEquals('b', $d->export_value());
}
/**
* Test getting field options, formatted
*/
public function test_get_options(): void {
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
$field = $this->get_generator()->create_field([
'categoryid' => $this->cfcat->get('id'),
'type' => 'select',
'shortname' => 'myselect',
'configdata' => [
'options' => <<<EOF
<span lang="en" class="multilang">Beginner</span><span lang="es" class="multilang">Novato</span>
<span lang="en" class="multilang">Intermediate</span><span lang="es" class="multilang">Intermedio</span>
<span lang="en" class="multilang">Advanced</span><span lang="es" class="multilang">Avanzado</span>
EOF,
],
]);
$this->assertEquals([
'',
'Beginner',
'Intermediate',
'Advanced',
], $field->get_options());
}
/**
* Data provider for {@see test_parse_value}
*
* @return array
*/
public function parse_value_provider(): array {
public static function parse_value_provider(): array {
return [
['Red', 1],
['Blue', 2],
@@ -169,7 +200,6 @@ class plugin_test extends \advanced_testcase {
*
* @param string $value
* @param int $expected
* @return void
*
* @dataProvider parse_value_provider
*/
+3
View File
@@ -1,6 +1,9 @@
This files describes API changes in /customfield/*,
Information provided here is intended especially for developers.
=== 4.4.2 ===
* The customfield_select field controller `get_options` method now returns each option pre-formatted
=== 4.4 ===
* Enhance the handling of files embedded within textarea custom fields in the course backup and restore functionalities.
These files are now properly backed up when a course is backed up, and restored when the course is restored.