【问题标题】:TYPO3 - Retrieved TypoScript in itemsProcFunc are incompleteTYPO3 - itemsProcFunc 中检索到的 TypoScript 不完整
【发布时间】:2017-05-04 11:45:45
【问题描述】:

我有以下问题: 我们正在用一个自定义列覆盖 tt_content TCA,该列的配置中有一个 itemsProcFunc。在函数中,我们尝试检索 TypoScript-Settings,以便我们可以动态显示项目。问题是:在函数中我们没有收到所有的 TypoScript-Settings,但只有一些。

'itemsProcFunc' => 'Vendor\Ext\Backend\Hooks\TcaHook->addFields',

class TcaHook
{

        public function addFields($config){
            $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
            $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');

            $setup = $configurationManager->getConfiguration(
                \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT
            );
        }

$setup 现在不完整并且不包含完整的 TypoScript,例如缺少一些静态包含的 TypoScript。

在作曲家模式下使用 TYPO3 7 LTS (7.6.18)、PHP 7.0.*。

有人知道问题出在哪里吗?有什么替代品吗?

【问题讨论】:

  • “静态包含的 TypoScript”是什么意思?你的意思是添加了ExtensionManagementUtility::addStaticFile的打字稿?
  • 是的,并将其包含在您的模板中的包含静态字段中。
  • 无法重现您的问题。模板中添加的整个打字稿正在我的测试中加载。

标签: dependency-injection typo3 typoscript


【解决方案1】:

您可能误解了 TypoScipt 的用途。这是前端的一种配置方式。您提到的 Hook 在 TCA 中使用,它是 TYPO3 的 Backend 部分。 TypoScript 通常根本不用于后端相关的东西,因为它绑定到特定的页面模板记录。而在后端,有TSConfig,可以绑定到页面,也可以全局添加。你做错的另一件事是使用 ObjectManager 和 ConfigurationManager,它们是 extbase 的类,它们没有在后端初始化。我建议不要在 TCA 中使用 extbase,因为每个页面请求都会缓存和加载 TCA。而是使用 TSConfig 或将您的配置设置直接提供给 TCA。不要初始化 extbase,也不要在这些钩子中使用 extbase 类。 根据您想通过 TypoScript 配置的内容,您可能需要执行以下操作:

'config' => [
    'type' => 'select',
    'renderType' => 'singleSelect',
    'items' => [
        ['EXT:my_ext/Resources/Private/Language/locallang_db.xlf:myfield.I.0', '']
    ],
    'itemsProcFunc' => \VENDOR\MyExt\UserFunctions\FormEngine\TypeSelectProcFunc::class . '->fillSelect',
    'customSetting' => 'somesetting'
]

然后在您的班级中访问它:

class TypeSelectProcFunc{
    public function fillSelect(&$params){
        if( $params['customSetting'] === 'somesetting' ){
            $params['items'][] = ['New item',1];
        }
    }
}

【讨论】:

    【解决方案2】:

    我遇到了类似的问题(itemsProcFunc 和检索 TypoScript)。就我而言,ConfigurationManager 不知道所选后端页面的当前页面 ID。因此,它使用了根页面的页面 id(例如 1)并且没有加载一些 TypoScript 模板。


    不过,在我们研究解决方案之前,Euli 提出了一些好的观点in his answer

    • 不要在 TCA 函数中使用 extbase 配置管理器
    • 使用 TSconfig 而不是 TypoScript 进行后端配置

    您可能想问另一个问题,您具体要做什么以及为什么在 BE 上下文中需要 TypoScript。


    为了完整起见,我测试了这个解决方法,但我不推荐它,因为上述原因以及我不确定这是否是最佳做法。 (我只使用它是因为我正在修补一个已经在 TCA 中使用 TypoScript 的扩展,我想找出它为什么不工作。我可能会完全重做这部分。)

    我发布这个是希望它对类似的问题有所帮助。

    public function populateItemsProcFunc(array &$config): array
    {
        // workaround to set current page id for BackendConfigurationManager
        $_GET['id'] = $this->getPageId((int)($config['flexParentDatabaseRow']['pid'] ?? 0));
    
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
        $configurationManager = $objectManager->get(BackendConfigurationManager::class);
        $setting = $configurationManager->getTypoScriptSetup();
        $templates = $setting['plugin.']['tx_rssdisplay.']['settings.']['templates.'] ?? [];
    
        // ... some code removed
    }
    
    protected function getPageId(int $pid): int
    {
       if ($pid > 0) {
           return $pid;
       }
    
       $row = BackendUtility::getRecord('tt_content', abs($pid), 'uid,pid');
       return $row['pid'];
    

    }

    函数getPageId() 派生自 ext:news,它也在 itemsProcFunc 中使用它,但它随后从 TSconfig 中检索配置。您可能还想查看一个示例:ext:news GeorgRinger\News\Hooks\ItemsProcFunc::user_templateLayout

    如果你看TYPO3核心中的代码,它会尝试从中获取当前页面id

    (int)GeneralUtility::_GP('id');
    

    https://github.com/TYPO3/TYPO3.CMS/blob/90fa470e37d013769648a17a266eb3072dea4f56/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php#L132

    这通常会被设置,但在 itemsProcFunc 中可能不会(我在 TYPO3 10.4.14 中就是这种情况)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多