[BUGFIX] Resolve workspace records for template module overview 82/57982/5
authorOliver Hader <oliver@typo3.org>
Tue, 21 Aug 2018 10:45:20 +0000 (12:45 +0200)
committerAndreas Fernandez <a.fernandez@scripting-base.de>
Mon, 3 Sep 2018 20:51:31 +0000 (22:51 +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/57982
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
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>
typo3/sysext/tstemplate/Classes/Controller/TypoScriptTemplateModuleController.php

index 60fc9db..38ba4e5 100644 (file)
@@ -20,8 +20,9 @@ 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\Http\HtmlResponse;
@@ -30,6 +31,7 @@ use TYPO3\CMS\Core\Type\Bitmask\Permission;
 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;
 
@@ -202,21 +204,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')
@@ -224,6 +236,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);
@@ -642,6 +658,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
      */