MDL-74500 user: method to determine whether to show profile fields.

The new API replaces identical behaviour in existing calling code, but
allows for profile field types to override/separate the logic used to
show the field and determine whether it's empty
This commit is contained in:
Paul Holden 2023-12-18 12:56:31 +00:00
parent f30110b5eb
commit 0ac237d542
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
4 changed files with 17 additions and 3 deletions

View File

@ -333,7 +333,7 @@ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user,
$categories = profile_get_user_fields_with_data_by_category($user->id);
foreach ($categories as $categoryid => $fields) {
foreach ($fields as $formfield) {
if ($formfield->is_visible() and !$formfield->is_empty()) {
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());
$tree->add_node($node);

View File

@ -399,8 +399,7 @@ function user_get_user_details($user, $course = null, array $userfields = array(
$userdetails['customfields'] = array();
foreach ($categories as $categoryid => $fields) {
foreach ($fields as $formfield) {
if ($formfield->is_visible() and !$formfield->is_empty()) {
if ($formfield->show_field_content()) {
$userdetails['customfields'][] = [
'name' => $formfield->field->name,
'value' => $formfield->data,

View File

@ -587,6 +587,16 @@ class profile_field_base {
return array(PARAM_RAW, NULL_NOT_ALLOWED);
}
/**
* Whether to display the field and content to the user
*
* @param context|null $context
* @return bool
*/
public function show_field_content(?context $context = null): bool {
return $this->is_visible($context) && !$this->is_empty();
}
/**
* Check if the field should convert the raw data into user-friendly data when exporting
*

View File

@ -1,5 +1,10 @@
This files describes API changes for code that uses the user API.
=== 4.4 ===
* The `profile_field_base` class now contains a `show_field_content` method to determine whether the field and
content should be shown to the user. Can be overridden in child classes as required
=== 4.3 ===
* Added new methods: