From fc2df2aa2e428ed0194d833234925214032fe17c Mon Sep 17 00:00:00 2001
From: Oliver Bartsch <bo@cedev.de>
Date: Tue, 3 Jan 2023 13:52:43 +0100
Subject: [PATCH] [BUGFIX] Always display search box in file browser

The search box in the file browser is now displayed again,
even if no files are in the folder. This is necessary because
the search works recursively.

Actually, the search box was only displayed for an active search
with an empty result list, due to another bug. FileSearchResultInterface
is never "empty" since it's an object. This is now fixed as well.

Additionally, the flash message is improved, in case no files
are available for a search term.

Resolves: #99447
Releases: main, 11.5
Change-Id: I970b19c7779075711f97987e80aad3f376cd5f40
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77268
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
---
 Build/phpstan/phpstan-baseline.neon           |  2 +-
 .../Classes/Browser/FileBrowser.php           | 36 ++++++++++++++-----
 .../Language/locallang_browse_links.xlf       |  3 ++
 3 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon
index 071744be9428..2cdcde9298a0 100644
--- a/Build/phpstan/phpstan-baseline.neon
+++ b/Build/phpstan/phpstan-baseline.neon
@@ -4932,7 +4932,7 @@ parameters:
 
 		-
 			message: "#^Call to an undefined method TYPO3\\\\CMS\\\\Core\\\\Resource\\\\FolderInterface\\:\\:getReadablePath\\(\\)\\.$#"
-			count: 2
+			count: 3
 			path: ../../typo3/sysext/recordlist/Classes/Browser/FileBrowser.php
 
 		-
diff --git a/typo3/sysext/recordlist/Classes/Browser/FileBrowser.php b/typo3/sysext/recordlist/Classes/Browser/FileBrowser.php
index 30b14a584868..dd38d928ae20 100644
--- a/typo3/sysext/recordlist/Classes/Browser/FileBrowser.php
+++ b/typo3/sysext/recordlist/Classes/Browser/FileBrowser.php
@@ -233,8 +233,33 @@ class FileBrowser extends AbstractElementBrowser implements ElementBrowserInterf
             $extensionList = !empty($extensionList) && $extensionList[0] === '*' ? [] : $extensionList;
             $files = $this->getFilesInFolder($folder, $extensionList);
         }
-        if (empty($files)) {
-            return $header . '<div class="shadow-sm bg-info bg-gradient p-3 mb-4 mt-4">' . sprintf(htmlspecialchars($lang->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf:no_files')), $folder->getStorage()->getName() . ':' . $folder->getReadablePath()) . '</div>';
+
+        // Prepare search box, since the component should always be displayed, even if no files are available
+        $searchBox = GeneralUtility::makeInstance(RecordSearchBoxComponent::class)
+            ->setSearchWord($this->searchWord)
+            ->render($this->getScriptUrl() . HttpUtility::buildQueryString($this->getUrlParameters([]), '&'));
+        $searchBox = '<div class="mt-4 mb-4">' . $searchBox . '</div>';
+
+        if (!count($files)) {
+            // No files found. Either due to an active search or simply because the folder is empty.
+            if ($this->searchWord !== '') {
+                $message = sprintf(
+                    $lang->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf:no_files_search'),
+                    $folder->getStorage()->getName() . ':' . $folder->getReadablePath(),
+                    $this->searchWord
+                );
+            } else {
+                $message = sprintf(
+                    $lang->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf:no_files'),
+                    $folder->getStorage()->getName() . ':' . $folder->getReadablePath()
+                );
+            }
+
+            return $header
+                . '<div class="shadow-sm bg-info bg-gradient p-3 mb-4 mt-4">' .
+                    htmlspecialchars($message)
+                . '</div>' .
+                $searchBox;
         }
         $lines = [];
 
@@ -331,13 +356,8 @@ class FileBrowser extends AbstractElementBrowser implements ElementBrowserInterf
 					</tr>';
         }
 
-        $formUrl = $this->getScriptUrl() . HttpUtility::buildQueryString($this->getUrlParameters([]), '&');
-        $searchBox = GeneralUtility::makeInstance(RecordSearchBoxComponent::class)
-            ->setSearchWord($this->searchWord)
-            ->render($formUrl);
-
         $markup = [];
-        $markup[] = '<div class="mt-4 mb-4">' . $searchBox . '</div>';
+        $markup[] = $searchBox;
         $markup[] = '<div id="filelist">';
         $markup[] = '  <div class="row row-cols-auto justify-content-between gx-0 list-header multi-record-selection-actions-wrapper">';
         $markup[] = '      <div class="col-auto">';
diff --git a/typo3/sysext/recordlist/Resources/Private/Language/locallang_browse_links.xlf b/typo3/sysext/recordlist/Resources/Private/Language/locallang_browse_links.xlf
index d8dff4983c7b..1d07fedb9148 100644
--- a/typo3/sysext/recordlist/Resources/Private/Language/locallang_browse_links.xlf
+++ b/typo3/sysext/recordlist/Resources/Private/Language/locallang_browse_links.xlf
@@ -138,6 +138,9 @@
 			<trans-unit id="no_files" resname="no_files">
 				<source>No files found in %s</source>
 			</trans-unit>
+			<trans-unit id="no_files_search" resname="no_files_search">
+				<source>No files found in %s for "%s"</source>
+			</trans-unit>
 		</body>
 	</file>
 </xliff>
-- 
GitLab