Use `FontAwesomeIcon` to provide an icon for menu items
authorMarcel Werk <burntime@woltlab.com>
Thu, 2 May 2024 14:44:03 +0000 (16:44 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 2 May 2024 14:44:03 +0000 (16:44 +0200)
See https://github.com/WoltLab/docs.woltlab.com/pull/430#discussion_r1587439823

wcfsetup/install/files/lib/system/menu/acp/ACPMenu.class.php
wcfsetup/install/files/lib/system/menu/acp/AcpMenuItem.class.php

index ac6561840010876ec372290ea4279fd9c7a48030..efac0aa24acd8dfa522a842f794dc29950316500 100644 (file)
@@ -7,6 +7,7 @@ use wcf\system\event\EventHandler;
 use wcf\system\menu\acp\event\AcpMenuCollecting;
 use wcf\system\menu\ITreeMenuItem;
 use wcf\system\menu\TreeMenu;
+use wcf\system\style\FontAwesomeIcon;
 use wcf\system\WCF;
 
 /**
@@ -77,12 +78,23 @@ class ACPMenu extends TreeMenu
                     continue;
                 }
 
+                $icon = null;
+                if ($item->icon) {
+                    if (FontAwesomeIcon::isValidString($item->icon)) {
+                        $icon = FontAwesomeIcon::fromString($item->icon);
+                    } elseif (\str_starts_with($item->icon, 'fa-')) {
+                        // Safeguard to prevent legacy icons from breaking
+                        // the admin panel during the upgrade to 6.0.
+                        $icon = FontAwesomeIcon::fromString("question;true");
+                    }
+                }
+
                 $this->menuItems[$parentMenuItem][] = new AcpMenuItem(
                     $item->menuItem,
                     $item->__toString(),
                     $item->parentMenuItem,
                     $item->getLink(),
-                    $item->icon ?? ''
+                    $icon
                 );
             }
         }
index bb54e6541543c0e6755ff9b2f7cbdf0eb31ef3e4..b4ab1153842f74aced83084f822de9e1a23e95ef 100644 (file)
@@ -21,7 +21,7 @@ final class AcpMenuItem implements ITreeMenuItem
         public readonly string $title = '',
         public readonly string $parentMenuItem = '',
         public readonly string $link = '',
-        public readonly string $icon = ''
+        public readonly ?FontAwesomeIcon $icon = null
     ) {
     }
 
@@ -32,17 +32,7 @@ final class AcpMenuItem implements ITreeMenuItem
 
     public function getIcon(): ?FontAwesomeIcon
     {
-        if ($this->icon) {
-            if (FontAwesomeIcon::isValidString($this->icon)) {
-                return FontAwesomeIcon::fromString($this->icon);
-            } elseif (\str_starts_with($this->icon, 'fa-')) {
-                // Safeguard to prevent legacy icons from breaking
-                // the admin panel during the upgrade to 6.0.
-                return FontAwesomeIcon::fromString("question;true");
-            }
-        }
-
-        return null;
+        return $this->icon;
     }
 
     public function __toString()