Skip to content

Commit

Permalink
[BUGFIX] Fix shortcut title generation in EditDocumentController
Browse files Browse the repository at this point in the history
Since #96154, the shortcut button is invalid in case
no `$displayName` is provided. For such case, the
ButtonBar throws an unhandled exception. The
EditDocumentController previously failed to ensure
that the corresponding value is always properly
set, which therefore led to mentioned exception
in some cases.

To fix this, it's now ensured that `getShortcutTitle()`
does always return a valid title. In case something is
wrong, e.g. no valid query arguments are provided, a
default title is used.

Previously, the `$recordId` in `getShortcutTitle()` was
always directly casted to an int. However, since it is
also possible to edit multiple records, the $recordId`
might be a comma separated list of uids. This case is
now also properly handled.

Note: The `getShortcutTitle()` is since #94182 also
used for the module title. This might be a misuse and
will therefore be further evaluated in another patch.

Resolves: #96337
Related: #96154
Releases: main
Change-Id: I2ec6a5d2ab8d5263315e8fa7194bc887b084ae79
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72632
Tested-by: core-ci <typo3@b13.com>
Tested-by: Riccardo De Contardi <erredeco@gmail.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
  • Loading branch information
o-ba committed Dec 13, 2021
1 parent 742cc1d commit 9cc53bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 21 additions & 7 deletions typo3/sysext/backend/Classes/Controller/EditDocumentController.php
Expand Up @@ -2584,45 +2584,59 @@ protected function getShortcutTitle(ServerRequestInterface $request): string
{
$queryParameters = $request->getQueryParams();
$languageService = $this->getLanguageService();
$defaultTitle = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.edit');

if (!is_array($queryParameters['edit'] ?? false)) {
return '';
return $defaultTitle;
}

// @todo There may be a more efficient way in using FormEngine FormData.
// @todo Therefore, the button initialization however has to take place at a later stage.

$table = (string)key($queryParameters['edit']);
$tableTitle = $languageService->sL($GLOBALS['TCA'][$table]['ctrl']['title'] ?? '') ?: $table;
$recordId = (int)key($queryParameters['edit'][$table]);
$action = (string)($queryParameters['edit'][$table][$recordId] ?? '');
$identifier = (string)key($queryParameters['edit'][$table]);
$action = (string)($queryParameters['edit'][$table][$identifier] ?? '');

if ($action === 'new') {
return $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.createNew') . ' ' . $tableTitle;
}

if ($action === 'edit') {
if ($multiple = str_contains($identifier, ',')) {
// Multiple records are given, use the first one for further evaluation of e.g. the parent page
$recordId = (int)(GeneralUtility::trimExplode(',', $identifier, true)[0] ?? 0);
} else {
$recordId = (int)$identifier;
}
$record = BackendUtility::getRecord($table, $recordId) ?? [];
$recordTitle = BackendUtility::getRecordTitle($table, $record) ?? '';
if ($table === 'pages') {
return sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editPage'), $tableTitle, $recordTitle);
return $multiple
? $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editMultiplePages')
: sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editPage'), $tableTitle, $recordTitle);
}
if (!isset($record['pid'])) {
return $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.edit');
return $defaultTitle;
}
$pageId = (int)$record['pid'];
if ($pageId === 0) {
return sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editRecordRootLevel'), $tableTitle, $recordTitle);
return $multiple
? sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editMultipleRecordsRootLevel'), $tableTitle)
: sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editRecordRootLevel'), $tableTitle, $recordTitle);
}
$pageRow = BackendUtility::getRecord('pages', $pageId) ?? [];
$pageTitle = BackendUtility::getRecordTitle('pages', $pageRow);
if ($multiple) {
return sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editMultipleRecords'), $tableTitle, $pageTitle);
}
if ($recordTitle !== '') {
return sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editRecord'), $tableTitle, $recordTitle, $pageTitle);
}
return sprintf($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.editRecordNoTitle'), $tableTitle, $pageTitle);
}

return '';
return $defaultTitle;
}

/**
Expand Down
Expand Up @@ -304,6 +304,15 @@ Do you want to continue WITHOUT saving?</source>
<trans-unit id="labels.editRecordRootLevel" resname="labels.editRecordRootLevel">
<source>Edit %s "%s" on root level</source>
</trans-unit>
<trans-unit id="labels.editMultiplePages" resname="labels.editMultiplePages">
<source>Edit multiple pages</source>
</trans-unit>
<trans-unit id="labels.editMultipleRecords" resname="labels.editMultipleRecords">
<source>Edit multiple "%s" records on page "%s"</source>
</trans-unit>
<trans-unit id="labels.editMultipleRecordsRootLevel" resname="labels.editMultipleRecordsRootLevel">
<source>Edit multiple "%s" records on root level</source>
</trans-unit>
<trans-unit id="labels.rootLevel" resname="labels.rootLevel">
<source>root level</source>
</trans-unit>
Expand Down

0 comments on commit 9cc53bd

Please sign in to comment.