From 44801c448cb83b221409ec956a20b68cb37f1f1a Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 15 Jul 2024 17:34:54 +0100 Subject: [PATCH] MDL-82481 customfield_select: return field options pre-formatted. --- .../field/select/classes/data_controller.php | 11 ++---- .../field/select/classes/field_controller.php | 8 +++-- .../field/select/tests/plugin_test.php | 36 +++++++++++++++++-- customfield/upgrade.txt | 3 ++ 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/customfield/field/select/classes/data_controller.php b/customfield/field/select/classes/data_controller.php index 8f50bcb227c..0a7e6ed8b53 100644 --- a/customfield/field/select/classes/data_controller.php +++ b/customfield/field/select/classes/data_controller.php @@ -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; diff --git a/customfield/field/select/classes/field_controller.php b/customfield/field/select/classes/field_controller.php index 3b18762c07e..53017682891 100644 --- a/customfield/field/select/classes/field_controller.php +++ b/customfield/field/select/classes/field_controller.php @@ -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', diff --git a/customfield/field/select/tests/plugin_test.php b/customfield/field/select/tests/plugin_test.php index 337d9410b52..7cd8c664055 100644 --- a/customfield/field/select/tests/plugin_test.php +++ b/customfield/field/select/tests/plugin_test.php @@ -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' => <<BeginnerNovato + IntermediateIntermedio + AdvancedAvanzado + 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 */ diff --git a/customfield/upgrade.txt b/customfield/upgrade.txt index 0336e14e7e1..e616039f24d 100644 --- a/customfield/upgrade.txt +++ b/customfield/upgrade.txt @@ -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.