From c0afa63f6e7a798ec13a8e257d977eada22b3234 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 16 Jul 2024 11:27:21 +0100 Subject: [PATCH] MDL-82494 user: correct profile field name formatting method. Add optional `$escape` parameter to allow for unescaped return. --- lib/myprofilelib.php | 2 +- .../classes/local/helpers/user_profile_fields.php | 9 ++------- user/profile/lib.php | 10 ++++++---- user/tests/externallib_test.php | 4 ++++ user/upgrade.txt | 5 +++++ 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/myprofilelib.php b/lib/myprofilelib.php index 7917898a0f3..73790ce9b1f 100644 --- a/lib/myprofilelib.php +++ b/lib/myprofilelib.php @@ -335,7 +335,7 @@ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, foreach ($fields as $formfield) { if ($formfield->show_field_content()) { $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname, - format_string($formfield->field->name), null, null, $formfield->display_data()); + $formfield->display_name(), null, null, $formfield->display_data()); $tree->add_node($node); } } diff --git a/reportbuilder/classes/local/helpers/user_profile_fields.php b/reportbuilder/classes/local/helpers/user_profile_fields.php index 36cb0276b5c..74b416c7b6c 100644 --- a/reportbuilder/classes/local/helpers/user_profile_fields.php +++ b/reportbuilder/classes/local/helpers/user_profile_fields.php @@ -18,7 +18,6 @@ declare(strict_types=1); namespace core_reportbuilder\local\helpers; -use context_system; use core_text; use core_reportbuilder\local\filters\boolean_select; use core_reportbuilder\local\filters\date; @@ -169,9 +168,7 @@ class user_profile_fields { $columns[] = (new column( 'profilefield_' . core_text::strtolower($profilefield->field->shortname), - new lang_string('customfieldcolumn', 'core_reportbuilder', - format_string($profilefield->field->name, true, - ['escape' => false, 'context' => context_system::instance()])), + new lang_string('customfieldcolumn', 'core_reportbuilder', $profilefield->display_name(false)), $this->entityname )) ->add_joins($this->get_joins()) @@ -237,9 +234,7 @@ class user_profile_fields { $filter = (new filter( $classname, 'profilefield_' . core_text::strtolower($profilefield->field->shortname), - new lang_string('customfieldcolumn', 'core_reportbuilder', - format_string($profilefield->field->name, true, - ['escape' => false, 'context' => context_system::instance()])), + new lang_string('customfieldcolumn', 'core_reportbuilder', $profilefield->display_name(false)), $this->entityname, $fieldsql, $params diff --git a/user/profile/lib.php b/user/profile/lib.php index 8fa15e2bac6..fe67d0feed4 100644 --- a/user/profile/lib.php +++ b/user/profile/lib.php @@ -149,11 +149,13 @@ class profile_field_base { /** * Display the name of the profile field. * + * @param bool $escape * @return string */ - public function display_name(): string { - return format_text($this->field->name, FORMAT_MOODLE, [ - 'para' => false, + public function display_name(bool $escape = true): string { + return format_string($this->field->name, true, [ + 'context' => context_system::instance(), + 'escape' => $escape, ]); } @@ -1008,7 +1010,7 @@ function get_profile_field_list(): array { if (!isset($data[$categoryname])) { $data[$categoryname] = []; } - $data[$categoryname][$field->inputname] = $field->field->name; + $data[$categoryname][$field->inputname] = $field->display_name(); } } return $data; diff --git a/user/tests/externallib_test.php b/user/tests/externallib_test.php index 3b77ae6fd4d..a7615f965f3 100644 --- a/user/tests/externallib_test.php +++ b/user/tests/externallib_test.php @@ -194,6 +194,8 @@ final class externallib_test extends externallib_advanced_testcase { // Create complex user profile field supporting multi-lang. filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', true); + $name = 'Employment status'. 'Estado de Empleo'; $statuses = 'UE\nSE\nOtherOtro'; @@ -359,6 +361,8 @@ final class externallib_test extends externallib_advanced_testcase { // Create complex user profile field supporting multi-lang. filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', true); + $name = 'Employment status' . 'Estado de Empleo'; $statuses = 'UE\nSE\nOtherOtro'; diff --git a/user/upgrade.txt b/user/upgrade.txt index 67751c8736b..a1c851800ce 100644 --- a/user/upgrade.txt +++ b/user/upgrade.txt @@ -1,5 +1,10 @@ This files describes API changes for code that uses the user API. +=== 4.3.8 === + +* The user profile field `display_name()` method now accepts an optional `$escape` parameter to define whether to + escape the returned name + === 4.3.3 === * The `profile_field_base` class now contains a `show_field_content` method to determine whether the field and