From 37ea5a51d4a50f119efa3395ab10c1e369962d76 Mon Sep 17 00:00:00 2001
From: Nikita Hovratov <nikita.h@live.de>
Date: Mon, 31 Jul 2023 11:06:03 +0200
Subject: [PATCH] [BUGFIX] Fix label generation for inline files

The labels of inline FileReference fields were not concatenated
but overwritten, in a loop since #92427.

This patch corrects the concatenation and adds line-break divs,
otherwise the strings are on a single line.

Resolves: #99873
Related: #92427
Releases: main, 12.4, 11.5
Change-Id: I368cd0d2cbf7e9ed7fdd1d003f869cc894d6f13e
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80265
Tested-by: Nikita Hovratov <nikita.h@live.de>
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: core-ci <typo3@b13.com>
---
 .../Service/UserFileInlineLabelService.php    | 98 ++++++++++++-------
 1 file changed, 60 insertions(+), 38 deletions(-)

diff --git a/typo3/sysext/core/Classes/Resource/Service/UserFileInlineLabelService.php b/typo3/sysext/core/Classes/Resource/Service/UserFileInlineLabelService.php
index 9d2c30757a43..ab53b4550cf8 100644
--- a/typo3/sysext/core/Classes/Resource/Service/UserFileInlineLabelService.php
+++ b/typo3/sysext/core/Classes/Resource/Service/UserFileInlineLabelService.php
@@ -17,10 +17,10 @@ namespace TYPO3\CMS\Core\Resource\Service;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Resource\Exception\InvalidUidException;
 use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
 
 /**
  * User file inline label service
@@ -54,49 +54,71 @@ class UserFileInlineLabelService
         }
 
         // Configuration
-        $title = '';
-        foreach ($sysFileFields as $field) {
-            $value = '';
-            if ($field === 'title') {
-                if (isset($params['row']['title'])) {
-                    $fullTitle = $params['row']['title'];
-                } else {
-                    try {
-                        $metaDataRepository = GeneralUtility::makeInstance(MetaDataRepository::class);
-                        $metaData = $metaDataRepository->findByFileUid($fileRecord['uid']);
-                        $fullTitle = $metaData['title'] ?? '';
-                    } catch (InvalidUidException $e) {
-                        /**
-                         * We just catch the exception here
-                         * Reasoning: There is nothing an editor or even admin could do
-                         */
-                        $fullTitle = '';
-                    }
-                }
-
-                $value = BackendUtility::getRecordTitlePrep($fullTitle);
-            } else {
-                if (isset($params['row'][$field])) {
-                    $value = htmlspecialchars($params['row'][$field]);
-                } elseif (isset($fileRecord[$field])) {
-                    $value = BackendUtility::getRecordTitlePrep($fileRecord[$field]);
-                }
-            }
-            if ((string)$value === '') {
-                continue;
-            }
-            $labelText = (string)LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_file.' . $field);
-            $title = '<dt class="col text-truncate">' . htmlspecialchars($labelText) . '</dt><dd class="col">' . $value . '</dd>';
-            // In debug mode, add the table name to the record title
-            if ($this->getBackendUserAuthentication()->shallDisplayDebugInformation()) {
-                $title .= '<div class="col"><code class="m-0">[' . htmlspecialchars($params['table']) . ']</code></div>';
+        $value = '';
+        $recordTitle = $this->getTitleForRecord($params['row'], $fileRecord);
+        $recordName = $this->getLabelFieldForRecord($params['row'], $fileRecord, 'name');
+
+        $labelField = !empty($recordTitle) ? 'title' : 'name';
+
+        if (!empty($recordTitle)) {
+            $value .= $recordTitle . ' (' . $recordName . ')';
+        } else {
+            $value .= $recordName;
+        }
+
+        $title = '
+            <dt class="col-1">
+                ' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_file.' . $labelField)) . '
+            </dt>
+            <dd class="col text-truncate">
+                ' . $value . '
+            </dd>
+            <div class="w-100"></div>';
+
+        // In debug mode, add the table name to the record title
+        if ($this->getBackendUserAuthentication()->shallDisplayDebugInformation()) {
+            $title .= '<div class="col"><code class="m-0">[' . htmlspecialchars($params['table']) . ']</code></div>';
+        }
+
+        $params['title'] = '<dl class="row row-cols-auto">' . $title . '</dl>';
+    }
+
+    protected function getTitleForRecord(array $databaseRow, array $fileRecord): string
+    {
+        $fullTitle = '';
+        if (isset($databaseRow['title'])) {
+            $fullTitle = $databaseRow['title'];
+        } else {
+            try {
+                $metaDataRepository = GeneralUtility::makeInstance(MetaDataRepository::class);
+                $metaData = $metaDataRepository->findByFileUid($fileRecord['uid']);
+                $fullTitle = $metaData['title'] ?? '';
+            } catch (InvalidUidException $e) {
             }
         }
-        $params['title'] = '<dl class="row row-cols-auto g-2">' . $title . '</dl>';
+
+        return BackendUtility::getRecordTitlePrep($fullTitle);
+    }
+
+    protected function getLabelFieldForRecord(array $databaseRow, array $fileRecord, string $field): string
+    {
+        $value = '';
+        if (isset($databaseRow[$field])) {
+            $value = htmlspecialchars($databaseRow[$field]);
+        } elseif (isset($fileRecord[$field])) {
+            $value = BackendUtility::getRecordTitlePrep($fileRecord[$field]);
+        }
+
+        return $value;
     }
 
     protected function getBackendUserAuthentication(): BackendUserAuthentication
     {
         return $GLOBALS['BE_USER'];
     }
+
+    protected function getLanguageService(): LanguageService
+    {
+        return $GLOBALS['LANG'];
+    }
 }
-- 
GitLab