From 9b0de169971c0b12b1898ce048a2ccdbf0a85083 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 10 Dec 2024 16:33:08 +0100 Subject: [PATCH] Add option for a fallback value in user renderer --- .../renderer/UserColumnRenderer.class.php | 12 ++++++++ .../renderer/UserLinkColumnRenderer.class.php | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php index d610113057..31ba9310ee 100644 --- a/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php +++ b/wcfsetup/install/files/lib/system/gridView/renderer/UserColumnRenderer.class.php @@ -15,10 +15,22 @@ use wcf\util\StringUtil; */ class UserColumnRenderer extends DefaultColumnRenderer { + public function __construct( + public readonly string $fallbackValue = 'username' + ) {} + #[\Override] public function render(mixed $value, mixed $context = null): string { if (!$value) { + if ($this->fallbackValue) { + if (\is_array($context)) { + return StringUtil::encodeHTML($context[$this->fallbackValue] ?? ''); + } else { + return StringUtil::encodeHTML($context->{$this->fallbackValue} ?? ''); + } + } + return ''; } diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php index 374c7d3c74..4cd20e6e97 100644 --- a/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php +++ b/wcfsetup/install/files/lib/system/gridView/renderer/UserLinkColumnRenderer.class.php @@ -4,6 +4,7 @@ namespace wcf\system\gridView\renderer; use wcf\system\cache\runtime\AbstractRuntimeCache; use wcf\system\cache\runtime\UserRuntimeCache; +use wcf\util\StringUtil; /** * Formats the content of a column as a user link. The value of the column must be a user id. @@ -15,9 +16,36 @@ use wcf\system\cache\runtime\UserRuntimeCache; */ class UserLinkColumnRenderer extends ObjectLinkColumnRenderer { + public function __construct( + string $controllerClass = '', + array $parameters = [], + string $titleLanguageItem = '', + public readonly string $fallbackValue = 'username' + ) { + parent::__construct($controllerClass, $parameters, $titleLanguageItem); + } + #[\Override] protected function getRuntimeCache(): AbstractRuntimeCache { return UserRuntimeCache::getInstance(); } + + #[\Override] + public function render(mixed $value, mixed $context = null): string + { + if ($value) { + return parent::render($value); + } + + if ($this->fallbackValue) { + if (\is_array($context)) { + return StringUtil::encodeHTML($context[$this->fallbackValue] ?? ''); + } else { + return StringUtil::encodeHTML($context->{$this->fallbackValue} ?? ''); + } + } + + return ''; + } } -- 2.20.1