Custom icon for search result items (#5461)
authorMarcel Werk <burntime@woltlab.com>
Thu, 27 Apr 2023 16:06:18 +0000 (18:06 +0200)
committerGitHub <noreply@github.com>
Thu, 27 Apr 2023 16:06:18 +0000 (18:06 +0200)
Closes #5109

com.woltlab.wcf/templates/searchResultList.tpl
wcfsetup/install/files/lib/data/search/SearchAction.class.php
wcfsetup/install/files/lib/system/search/AbstractSearchProvider.class.php
wcfsetup/install/files/lib/system/search/ISearchProvider.class.php
wcfsetup/install/files/lib/system/search/SearchResultHandler.class.php

index d8c6aa06b174c0b0d524b4f6236111922baeba69..33c6b438c6c6b4444833f610927e59686055d5e9 100644 (file)
@@ -3,7 +3,9 @@
                {foreach from=$objects item=message}
                        <li class="gridListItem gridListItemMessage">
                                <div class="gridListItemImage">
-                                       {if $message->getUserProfile()}
+                                       {if $customIcons[$message->getObjectTypeName()]|isset}
+                                               {icon size=48 name=$customIcons[$message->getObjectTypeName()]}
+                                       {elseif $message->getUserProfile()}
                                                {user object=$message->getUserProfile() type='avatar48' ariaHidden='true' tabindex='-1'}
                                        {else}
                                                {icon size=48 name='file'}
index 2e4dd99f5aad6cc6da63c68bdf1864efc82f4871..f21357cfcbb636d8ac335cf9174136411a7ae8dc 100644 (file)
@@ -120,6 +120,7 @@ class SearchAction extends AbstractDatabaseObjectAction
         WCF::getTPL()->assign([
             'objects' => $resultHandler->getSearchResults(),
             'query' => $resultHandler->getQuery(),
+            'customIcons' => $resultHandler->getCustomIcons(),
         ]);
 
         return [
@@ -166,6 +167,7 @@ class SearchAction extends AbstractDatabaseObjectAction
         WCF::getTPL()->assign([
             'objects' => $resultHandler->getSearchResults(),
             'query' => $resultHandler->getQuery(),
+            'customIcons' => $resultHandler->getCustomIcons(),
         ]);
 
         return [
index 421e1b1d5449fed009d53426deac642f09b8ec20..4577bbbecc16df15bf482ab57bb91b8923979203 100644 (file)
@@ -120,6 +120,14 @@ abstract class AbstractSearchProvider extends AbstractObjectTypeProcessor implem
         return '';
     }
 
+    /**
+     * @inheritDoc
+     */
+    public function getCustomIconName(): ?string
+    {
+        return null;
+    }
+
     /**
      * @deprecated 5.5
      */
index fd71b442220a2bfe518b7dffd6d20c4eaa4687ba..1a1c3f8fec2d8af3bc6bb636f015d567912a229c 100644 (file)
@@ -102,4 +102,11 @@ interface ISearchProvider
      * placeholder {WCF_SEARCH_INNER_JOIN} within an empty INNER JOIN() statement.
      */
     public function getFetchObjectsQuery(?PreparedStatementConditionBuilder $additionalConditions = null): string;
+
+    /**
+     * Provides the ability to show a custom icon in the list of search results.
+     * Returns either a FontAwesome icon name or null. The latter will trigger the default behavior.
+     * @since 6.0
+     */
+    public function getCustomIconName(): ?string;
 }
index 6507e055043c492ab5aa1c592aaaad9865ee11ea..bbd3b601ed6699ac31ecab7c2f995fbcfbb963b2 100644 (file)
@@ -138,6 +138,21 @@ final class SearchResultHandler
         return $this->getLegacyTemplateName();
     }
 
+    /**
+     * @return array<string, string|null>
+     */
+    public function getCustomIcons(): array
+    {
+        $customIcons = [];
+        foreach (SearchEngine::getInstance()->getAvailableObjectTypes() as $name => $type) {
+            if ($type instanceof ISearchProvider) {
+                $customIcons[$name] = $type->getCustomIconName();
+            }
+        }
+
+        return $customIcons;
+    }
+
     /**
      * Will be removed with 6.0 once all search providers have switched to ISearchProvider.
      * @deprecated 5.5