MDL-77274 core_renderer: Respect full name format in letter avatars
* Make sure that we respect the fullnamedisplay and alternativefullnameformat parameters to decide on the initials for a given user * Add further tests Co-authored-by: Tatsunori Uchino <[email protected]>
This commit is contained in:
committed by
Laurent David
co-authored by
Tatsunori Uchino
parent
933c9c0756
commit
bbd015f89b
@@ -1226,4 +1226,31 @@ class core_user {
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get initials for users
|
||||
*
|
||||
* @param stdClass $user
|
||||
* @return string
|
||||
*/
|
||||
public static function get_initials(stdClass $user): string {
|
||||
// Get the available name fields.
|
||||
$namefields = \core_user\fields::get_name_fields();
|
||||
// Build a dummy user to determine the name format.
|
||||
$dummyuser = array_combine($namefields, $namefields);
|
||||
// Determine the name format by using fullname() and passing the dummy user.
|
||||
$nameformat = fullname((object) $dummyuser);
|
||||
// Fetch all the available username fields.
|
||||
$availablefields = order_in_string($namefields, $nameformat);
|
||||
// We only want the first and last name fields.
|
||||
if (!empty($availablefields) && count($availablefields) >= 2) {
|
||||
$availablefields = [reset($availablefields), end($availablefields)];
|
||||
}
|
||||
$initials = '';
|
||||
foreach ($availablefields as $userfieldname) {
|
||||
$initials .= mb_substr($user->$userfieldname, 0, 1);
|
||||
}
|
||||
return $initials;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user