Add renderer for truncated text
authorMarcel Werk <burntime@woltlab.com>
Tue, 19 Nov 2024 15:16:07 +0000 (16:16 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 19 Nov 2024 15:16:07 +0000 (16:16 +0100)
wcfsetup/install/files/lib/system/gridView/renderer/TruncatedTextColumnRenderer.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/gridView/renderer/TruncatedTextColumnRenderer.class.php b/wcfsetup/install/files/lib/system/gridView/renderer/TruncatedTextColumnRenderer.class.php
new file mode 100644 (file)
index 0000000..e6299ed
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace wcf\system\gridView\renderer;
+
+use wcf\util\StringUtil;
+
+/**
+ * Truncates the content of a column to a length of 80 characters (default value).
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+class TruncatedTextColumnRenderer extends DefaultColumnRenderer
+{
+    public function __construct(
+        private readonly int $length = 80,
+        private readonly string $etc = "\u{2026}"
+    ) {}
+
+    #[\Override]
+    public function render(mixed $value, mixed $context = null): string
+    {
+        $renderedValue = StringUtil::encodeHTML(StringUtil::truncate($value, $this->length, $this->etc));
+
+        if (\mb_strlen($value) > $this->length) {
+            $renderedValue = '<span title="' . StringUtil::encodeHTML($value) . '">' . $renderedValue . '</span>';
+        }
+
+        return $renderedValue;
+    }
+}