TYPO3 10.4.6:无需修改ext_conf_template.txt实现selectMultipleSideBySide字段
TYPO3 10.4.6 扩展配置添加selectMultipleSideBySide字段的问题
我使用的TYPO3版本为10.4.6,项目中有一个带ext_conf_template.txt的自定义扩展。按官方文档,通过user选项类型关联自定义用户函数可以实现特定字段,但尝试后失败了。想问有没有无需编辑ext_conf_template.txt文件的方法,在扩展配置中创建selectMultipleSideBySide字段?
尝试的错误代码
ext_conf_template.txt 配置
# cat=basic/enable; type=user[My\Path\UserFunctions\ExtConfUserFunctions->renderCategories]; label=Kategorien ausschließen excluded =
自定义用户函数
<?php namespace My\Path\UserFunctions; use TYPO3\CMS\Backend\Form\Element\SelectMultipleSideBySideElement; use TYPO3\CMS\Backend\Form\NodeFactory; use TYPO3\CMS\Core\Utility\GeneralUtility; class ExtConfUserFunctions { /** * @return string */ public function renderCategories(): string { /** @var NodeFactory $nodeFactory */ $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class); /** @var SelectMultipleSideBySideElement $element */ $element = GeneralUtility::makeInstance( SelectMultipleSideBySideElement::class, $nodeFactory, [ 'config' => [ 'type' => 'select', 'renderType' => 'selectMultipleSideBySide', 'items' => [ ['Option 1', '1'], ['Option 2', '2'], ['Option 3', '3'], ] ] ] ); $elementArray = $element->render(); } }
解决方案
方案1:修复现有用户函数
你的用户函数存在两个核心问题:未返回渲染结果、未处理当前配置值。修改后的代码如下:
<?php namespace My\Path\UserFunctions; use TYPO3\CMS\Backend\Form\Element\SelectMultipleSideBySideElement; use TYPO3\CMS\Backend\Form\NodeFactory; use TYPO3\CMS\Core\Utility\GeneralUtility; class ExtConfUserFunctions { /** * @param array $params 自动传入的字段配置与当前值 * @return string */ public function renderCategories(array $params): string { $currentValue = $params['fieldValue']; /** @var NodeFactory $nodeFactory */ $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class); $elementConfig = [ 'tableName' => 'sys_template', 'fieldName' => $params['fieldName'], 'databaseRow' => ['value' => $currentValue], 'parameterArray' => [ 'itemFormElName' => $params['itemFormElName'], 'itemFormElValue' => $currentValue, 'fieldConf' => [ 'config' => [ 'type' => 'select', 'renderType' => 'selectMultipleSideBySide', 'items' => [ ['Option 1', '1'], ['Option 2', '2'], ['Option 3', '3'], ], 'size' => 5, 'multiple' => true, ], ], ], ]; $element = GeneralUtility::makeInstance(SelectMultipleSideBySideElement::class, $nodeFactory, $elementConfig); $renderResult = $element->render(); return $renderResult['html']; } }
修改后,ext_conf_template.txt的配置保持不变即可正常工作。
方案2:无需编辑ext_conf_template.txt的替代方案
TYPO3 10.4+支持通过Configuration/ExtensionConfiguration.php直接定义扩展配置的表单结构,无需依赖ext_conf_template.txt和自定义用户函数。
在你的扩展目录下创建Configuration/ExtensionConfiguration.php文件,内容如下:
<?php return [ 'excluded' => [ 'label' => 'Kategorien ausschließen', 'type' => 'select', 'renderType' => 'selectMultipleSideBySide', 'items' => [ ['Option 1', '1'], ['Option 2', '2'], ['Option 3', '3'], ], 'size' => 5, 'multiple' => true, 'default' => '', ], ];
完成后,删除ext_conf_template.txt中对应的excluded配置行,TYPO3会自动读取该文件生成对应的selectMultipleSideBySide字段,这是官方推荐的更灵活的配置方式。
内容的提问来源于stack exchange,提问作者EmWe
相关产品推荐
相关产品推荐

