如何在 TYPO3 v10 中访问 $GLOBALS['TSFE']

问题描述

以下代码段适用于版本 8 和 9 的插件。

基本上这用于从根模板获取排版设置。

 protected function getTypoScriptSetup()
    {
        

        /** @var \TYPO3\CMS\Frontend\Page\PageRepository $pageRepository */

        $pageRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
        $pageRepository->init(false);

        /** @var \TYPO3\CMS\Core\TypoScript\TemplateService $templateService */

        $templateService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\TemplateService::class);
        $typo3Branch = class_exists(\TYPO3\CMS\Core\Information\Typo3Version::class)
            ? (new \TYPO3\CMS\Core\Information\Typo3Version())->getBranch()
            : TYPO3_branch;

        if (version_compare($typo3Branch,'9.0','<')) {
            $templateService->init();
        }

        $templateService->tt_track = false;

        $currentPage = $GLOBALS['TSFE']->id;

        if ($currentPage === null) {
            // root page is not yet populated
            $localTSFE = clone $GLOBALS['TSFE'];

        if (version_compare($typo3Branch,'9.5','>=')) {
                $localTSFE->fe_user = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
            }
            $localTSFE->determineId();
            $currentPage = $localTSFE->id;
        }

        if (version_compare($typo3Branch,'>=')) {
            $rootLine = GeneralUtility::makeInstance(RootlineUtility::class,(int)$currentPage)->get();
        } else {
            $rootLine = $pageRepository->getRootLine((int)$currentPage);
        }
        $templateService->start($rootLine);

        $setup = $templateService->setup;
        return $setup;
    }

我已经坚持了好几天但没有运气。 在 10.4 版本中,我在克隆 $GLOBAL['TSFE'] 处收到一条错误消息。

错误:“_clone 在非对象上”

我需要获取当前页面信息才能从模板中成功获取排版设置。任何其他方式来实现这一点也欢迎。我想我在 v10 更新日志中遗漏了一些东西。

解决方法

使用上下文 API 代替 $GLOBALS['TSFE']
请参阅documentation

由于您没有说明哪些数据对您很重要,因此无法提供进一步的帮助。
更具体可以让其他人更有帮助。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...