TYPO3 V12 EXT:form如何预填当前对象自定义ID至表单字段
解决方案:在TYPO3 V12表单中预填自定义Object ID
核心思路
基于你已能获取URL中的对象UID,我们需要通过UID查询tx_myext_object_table表拿到自定义object_id,再将其传递给表单字段作为默认值。以下是两种可靠实现方式:
方式一:控制器传值(推荐,适用于对象详情页嵌入表单)
如果表单是在对象详情页(如tx_myext_object_table的show动作页面)嵌入,直接从当前对象模型中获取object_id并传递到模板:
1. 修改控制器代码
在你的扩展控制器(如Classes/Controller/ObjectController.php)中,将object_id赋值到模板:
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Database\ConnectionPool; public function showAction(\Vendor\MyExt\Domain\Model\Object $object) { // 方式1:如果Domain Model已映射object_id字段(推荐) $objectId = $object->getObjectId(); // 方式2:未映射字段时,直接查询数据库 // $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_myext_object_table'); // $objectId = $queryBuilder // ->select('object_id') // ->from('tx_myext_object_table') // ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($object->getUid(), \PDO::PARAM_INT))) // ->executeQuery() // ->fetchOne(); // 将值传递到模板 $this->view->assign('objectId', $objectId); return $this->htmlResponse(); }
2. 模板中传递默认值给表单
使用Form Framework的<form:render>标签,通过overrideSettings设置字段默认值:
{namespace form=TYPO3\CMS\Form\ViewHelpers} <form:render persistenceIdentifier="EXT:myext/Resources/Private/Forms/RequestMoreInfo.form.yaml" overrideSettings="{ 'renderingOptions': { 'defaultValues': { 'requestMoreInfo': { // 替换为你的表单identifier 'object_id': '{objectId}' // 替换为你的字段identifier } } } }" />
方式二:表单前置处理器(通用,适用于任意嵌入场景)
如果需要在任意页面嵌入表单,只要URL带对象UID就能自动填充object_id,可以自定义表单前置处理器:
1. 创建前置处理器类
在Classes/Form/PreProcessor/LoadObjectIdPreProcessor.php中编写逻辑:
namespace Vendor\MyExt\Form\PreProcessor; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Form\Domain\Runtime\FormRuntime; use TYPO3\CMS\Form\Domain\Configuration\ConfigurationServiceInterface; class LoadObjectIdPreProcessor implements \TYPO3\CMS\Form\Domain\Processor\PreProcessorInterface { public function process(FormRuntime $formRuntime, array $processorConfiguration, ConfigurationServiceInterface $configurationService) { // 从URL获取对象UID(根据你的路由参数调整) $uid = (int)GeneralUtility::_GET('tx_myext_object[object]'); if (!$uid) return; // 查询数据库获取object_id $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_myext_object_table'); $objectId = $queryBuilder ->select('object_id') ->from('tx_myext_object_table') ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))) ->executeQuery() ->fetchOne(); // 设置表单字段值 if ($objectId) { $formRuntime->getFormState()->setFormValue('requestMoreInfo.object_id', $objectId); } } }
2. 在表单配置中注册处理器
编辑你的表单YAML文件(如RequestMoreInfo.form.yaml),添加前置处理器:
type: Form identifier: requestMoreInfo label: 更多信息请求 # ...其他表单元素配置 preProcessors: - identifier: Vendor\MyExt\Form\PreProcessor\LoadObjectIdPreProcessor options: {}
表单字段配置建议
将object_id设为隐藏字段,避免用户修改:
elements: - type: Hidden identifier: object_id label: 对象自定义ID properties: readonly: true
在邮件finishers中可以直接使用{formState.formValues.object_id}获取该值,比如:
finishers: - identifier: EmailToReceiver options: subject: 对象{formState.formValues.object_id}的更多信息请求 htmlBody: | <p>请求对象ID:{formState.formValues.object_id}</p> <!-- 其他邮件内容 -->
内容的提问来源于stack exchange,提问作者webman
相关产品推荐
相关产品推荐

