diff --git a/Classes/Persistence/Generic/Storage/Typo3DbBackend.php b/Classes/Persistence/Generic/Storage/Typo3DbBackend.php index 836fbdaa..88fa80b5 100644 --- a/Classes/Persistence/Generic/Storage/Typo3DbBackend.php +++ b/Classes/Persistence/Generic/Storage/Typo3DbBackend.php @@ -546,8 +546,10 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface { if ($source instanceof Qom\SelectorInterface) { $tableName = $source->getSelectorName(); + $forceRecordReloadInWorkspace = false; } elseif ($source instanceof Qom\JoinInterface) { $tableName = $source->getRight()->getSelectorName(); + $forceRecordReloadInWorkspace = true; } else { // No proper source, so we do not have a table name here // we cannot do an overlay and return the original rows instead. @@ -569,9 +571,9 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface // by the table and if there's only one row in the result set // (applying this to all rows does not work, since the sorting // order would be destroyed and possible limits not met anymore) - if (!empty($workspaceUid) - && BackendUtility::isTableWorkspaceEnabled($tableName) - && count($rows) === 1 + $isInWorkspaceContext = !empty($workspaceUid) + && BackendUtility::isTableWorkspaceEnabled($tableName); + if ($isInWorkspaceContext && count($rows) === 1 ) { $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); $queryBuilder->getRestrictions()->removeAll(); @@ -588,17 +590,23 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface ->fetch(); if (!empty($movePlaceholder)) { $rows = [$movePlaceholder]; + // Records have been "reloaded" + $forceRecordReloadInWorkspace = false; } } $overlaidRows = []; foreach ($rows as $row) { - // If current row is a translation select its parent + $originalRow = $row; + // If current row is a translation or we are in a workspace preview, select its parent if (isset($tableName) && isset($GLOBALS['TCA'][$tableName]) && isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) && isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']) && isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']]) - && $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0 + && ( + $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0 + || ($forceRecordReloadInWorkspace && $isInWorkspaceContext) + ) ) { $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); $queryBuilder->getRestrictions()->removeAll(); @@ -633,6 +641,11 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface $row = $pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode); } if (is_array($row)) { + if ($forceRecordReloadInWorkspace && $isInWorkspaceContext) { + // Add additional fields back to the overlaid record + $row = array_merge($originalRow, $row); + //$row = array_merge(...$row); + } $overlaidRows[] = $row; } } @@ -654,8 +667,10 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface { if ($source instanceof Qom\SelectorInterface) { $tableName = $source->getSelectorName(); + $forceRecordReloadInWorkspace = false; } elseif ($source instanceof Qom\JoinInterface) { $tableName = $source->getRight()->getSelectorName(); + $forceRecordReloadInWorkspace = true; } else { // No proper source, so we do not have a table name here // we cannot do an overlay and return the original rows instead. @@ -677,10 +692,9 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface // by the table and if there's only one row in the result set // (applying this to all rows does not work, since the sorting // order would be destroyed and possible limits not met anymore) - if (!empty($workspaceUid) - && BackendUtility::isTableWorkspaceEnabled($tableName) - && count($rows) === 1 - ) { + $isInWorkspaceContext = !empty($workspaceUid) + && BackendUtility::isTableWorkspaceEnabled($tableName); + if ($isInWorkspaceContext && count($rows) === 1) { $versionId = $workspaceUid; $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); $queryBuilder->getRestrictions()->removeAll(); @@ -697,10 +711,47 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface ->fetchAll(); if (!empty($movePlaceholder)) { $rows = $movePlaceholder; + // Records have been "reloaded" + $forceRecordReloadInWorkspace = false; } } $overlaidRows = []; foreach ($rows as $row) { + $originalRow = $row; + // If current row is a translation or we are in a workspace preview, select its parent + if (isset($tableName) && isset($GLOBALS['TCA'][$tableName]) + && isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) + && isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']) + && isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']]) + && ( + $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0 + || ($forceRecordReloadInWorkspace && $isInWorkspaceContext) + ) + ) { + $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); + $queryBuilder->getRestrictions()->removeAll(); + $row = $queryBuilder + ->select($tableName . '.*') + ->from($tableName) + ->where( + $queryBuilder->expr()->eq( + $tableName . '.uid', + $queryBuilder->createNamedParameter( + $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']], + \PDO::PARAM_INT + ) + ), + $queryBuilder->expr()->eq( + $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'], + $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT) + ) + ) + ->setMaxResults(1) + ->execute() + ->fetch(); + } + + $pageRepository->versionOL($tableName, $row, true); $querySettings = $query->getQuerySettings(); if (is_array($row) && $querySettings->getLanguageOverlayMode()) { @@ -728,6 +779,10 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface } } if ($row !== null && is_array($row)) { + if ($forceRecordReloadInWorkspace && $isInWorkspaceContext) { + // Add additional fields back to the overlaid record + $row = array_merge($originalRow, $row); + } $overlaidRows[] = $row; } }