TYPO3 - itemsProcFunc 中检索到的 TypoScript 不完整
TYPO3 - Retrieved TypoScript in itemsProcFunc are incomplete
我有以下问题:
我们正在使用自定义列覆盖 tt_content TCA,该列在其配置中包含 itemsProcFunc。在函数中,我们尝试检索 TypoScript-Settings,因此我们可以动态显示项目。问题是:在函数中我们没有收到所有的 TypoScript 设置,这些设置包含在内,但只有一部分。
'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.*。
有人知道问题出在哪里吗?有其他选择吗?
您可能误解了 TypoScipt 的用途。这是 Frontend 的一种配置方式。你提到的 Hook 在 TCA 中使用,它是 TYPO3 的 Backend 部分。 TypoScript 通常根本不用于后端相关的东西,因为它绑定到特定的页面模板记录。相反,在后端,有 TSConfig,可以绑定到页面,也可以全局添加。您做错的另一件事是使用 ObjectManager 和 ConfigurationManager,它们是 extbase 的 classes,未在后端初始化。我建议不要在 TCA 中使用 extbase,因为 TCA 会针对每个页面请求进行缓存和加载。而是使用 TSConfig 或将您的配置设置直接提供给 TCA。不要初始化 extbase,也不要在这些挂钩中使用 extbase classes。
根据你想通过 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:
中访问它
class TypeSelectProcFunc{
public function fillSelect(&$params){
if( $params['customSetting'] === 'somesetting' ){
$params['items'][] = ['New item',1];
}
}
}
我遇到了类似的问题(还有 itemsProcFunc
和检索 TypoScript)。在我的例子中,ConfigurationManager 不知道所选后端页面的当前页面 ID。因此,它使用了根页面的页面 ID(例如 1),并且未加载一些 TypoScript 模板。
然而,在我们查看解决方案之前,Euli 提出了一些很好的观点 :
- 不要在 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');
这通常会被设置,但在 itemsProcFunc 中它可能不会(我在 TYPO3 10.4.14 中就是这种情况)。
我有以下问题: 我们正在使用自定义列覆盖 tt_content TCA,该列在其配置中包含 itemsProcFunc。在函数中,我们尝试检索 TypoScript-Settings,因此我们可以动态显示项目。问题是:在函数中我们没有收到所有的 TypoScript 设置,这些设置包含在内,但只有一部分。
'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.*。
有人知道问题出在哪里吗?有其他选择吗?
您可能误解了 TypoScipt 的用途。这是 Frontend 的一种配置方式。你提到的 Hook 在 TCA 中使用,它是 TYPO3 的 Backend 部分。 TypoScript 通常根本不用于后端相关的东西,因为它绑定到特定的页面模板记录。相反,在后端,有 TSConfig,可以绑定到页面,也可以全局添加。您做错的另一件事是使用 ObjectManager 和 ConfigurationManager,它们是 extbase 的 classes,未在后端初始化。我建议不要在 TCA 中使用 extbase,因为 TCA 会针对每个页面请求进行缓存和加载。而是使用 TSConfig 或将您的配置设置直接提供给 TCA。不要初始化 extbase,也不要在这些挂钩中使用 extbase classes。 根据你想通过 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:
中访问它class TypeSelectProcFunc{
public function fillSelect(&$params){
if( $params['customSetting'] === 'somesetting' ){
$params['items'][] = ['New item',1];
}
}
}
我遇到了类似的问题(还有 itemsProcFunc
和检索 TypoScript)。在我的例子中,ConfigurationManager 不知道所选后端页面的当前页面 ID。因此,它使用了根页面的页面 ID(例如 1),并且未加载一些 TypoScript 模板。
然而,在我们查看解决方案之前,Euli 提出了一些很好的观点
- 不要在 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');
这通常会被设置,但在 itemsProcFunc 中它可能不会(我在 TYPO3 10.4.14 中就是这种情况)。