[BUGFIX] Resolve workspace records for template module overview 64/58164/2
authorOliver Hader <oliver@typo3.org>
Tue, 21 Aug 2018 10:45:20 +0000 (12:45 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 3 Sep 2018 21:24:17 +0000 (23:24 +0200)
BackendWorkspaceRestriction automatically resolves specific records
already for a given workspace. In most cases this seems to be fine to
omit the usual workspace overlay processing. However, especially when
dealing with references and page tree assignments, it just contains
wrong information.

For instance all workspace versions have the `pid` column set to -1
which of course cannot be resolved as valid page then.

Resolves: #85917
Releases: master, 8.7
Change-Id: Ibe20037b25770bea8f58d1a8bba785ca43ac26cb
Reviewed-on: https://review.typo3.org/58164
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/tstemplate/Classes/Controller/TypoScriptTemplateModuleController.php

index 1675ae1..e6e95dd 100644 (file)
@@ -20,14 +20,16 @@ use TYPO3\CMS\Backend\Module\BaseScriptClass;
 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
 use TYPO3\CMS\Backend\Template\ModuleTemplate;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
+use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
+use TYPO3\CMS\Core\Versioning\VersionState;
 use TYPO3\CMS\Fluid\View\StandaloneView;
 use TYPO3\CMS\Fluid\ViewHelpers\Be\InfoboxViewHelper;
 
@@ -205,21 +207,31 @@ class TypoScriptTemplateModuleController extends BaseScriptClass
             $this->getButtons();
             $this->generateMenu();
         } else {
-            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
+            $workspaceId = $this->getBackendUser()->workspace;
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getQueryBuilderForTable('sys_template');
             $queryBuilder->getRestrictions()
                 ->removeAll()
-                ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
-                ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
-
-            $result = $queryBuilder->select('sys_template.uid')
-                ->addSelect(
-                    'sys_template.pid',
-                    'sys_template.title',
-                    'sys_template.sitetitle',
-                    'sys_template.root',
-                    'sys_template.hidden',
-                    'sys_template.starttime',
-                    'sys_template.endtime'
+                ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+            $this->applyWorkspaceConstraint(
+                $queryBuilder,
+                'sys_template',
+                $workspaceId
+            );
+            $result = $queryBuilder
+                ->select(
+                    'uid',
+                    'pid',
+                    'title',
+                    'sitetitle',
+                    'root',
+                    'hidden',
+                    'starttime',
+                    'endtime',
+                    't3ver_oid',
+                    't3ver_wsid',
+                    't3ver_state',
+                    't3ver_move_id'
                 )
                 ->from('sys_template')
                 ->orderBy('sys_template.pid')
@@ -227,6 +239,10 @@ class TypoScriptTemplateModuleController extends BaseScriptClass
                 ->execute();
             $pArray = [];
             while ($record = $result->fetch()) {
+                BackendUtility::workspaceOL('sys_template', $record, $workspaceId, true);
+                if (empty($record) || VersionState::cast($record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
+                    continue;
+                }
                 $additionalFieldsForRootline = ['sorting', 'hidden', 'fe_group', 'starttime', 'endtime', 'shortcut', 'nav_hide', 'module', 'content_from_pid'];
                 $rootline = BackendUtility::BEgetRootLine($record['pid'], '', true, $additionalFieldsForRootline);
                 $this->setInPageArray($pArray, $rootline, $record);
@@ -683,6 +699,36 @@ page.10.value = HELLO WORLD!
     }
 
     /**
+     * @param QueryBuilder $queryBuilder
+     * @param string $tableName
+     * @param int $workspaceId
+     */
+    protected function applyWorkspaceConstraint(
+        QueryBuilder $queryBuilder,
+        string $tableName,
+        int $workspaceId
+    ) {
+        if (empty($GLOBALS['TCA'][$tableName]['ctrl']['versioningWS'])) {
+            return;
+        }
+
+        $workspaceIds = [0];
+        if ($workspaceId > 0) {
+            $workspaceIds[] = $workspaceId;
+        }
+        $queryBuilder->andWhere(
+            $queryBuilder->expr()->in(
+                't3ver_wsid',
+                $queryBuilder->createNamedParameter($workspaceIds, Connection::PARAM_INT_ARRAY)
+            ),
+            $queryBuilder->expr()->neq(
+                'pid',
+                $queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT)
+            )
+        );
+    }
+
+    /**
      * @param string $action
      * @return string
      */