Typo3 9.5如何使用GET参数填充表单字段

3

我使用TYPO3系统扩展“form”,想要使用GET参数预填一个输入字段。

这个 TYPO3 8.7. Form prefill input field 是有效的,但是只有在no_cache=1的情况下。是否有其他解决方案而不必禁用整个缓存?

谢谢 大卫

3个回答

1

是的,你可以,但你需要创建钩子

这在文档中有描述。

例如,钩子

/**
 * @param \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable
 * @return void
 */
public function initializeFormElement(\TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable)
{
    if ($renderable->getUniqueIdentifier() === 'contactForm-text-1') {
        $renderable->setDefaultValue('foo');
    }
}

然后连接钩子

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'][<useATimestampAsKeyPlease>]
    = \VENDOR\YourNamespace\YourClass::class;

请阅读“表单框架”的文档。我已经这样做并获得了所需的结果。

谢谢您的留言,但这并不能解决缓存问题。我该如何设置表单为“无缓存”,以填充字段,而不必禁用完整缓存? - david123456789

0
您可以禁用表单页面内容列的缓存,例如:
lib.content = COA
lib.content{
    10 < styles.content.get
}
[page["uid"] == 512]
    lib.content = COA_INT
[global]

在页面条件中使用 config.no_cache = 1 禁用缓存,这样不是更容易吗? - schnere
1
这也是可能的,但明确要求提供另一种解决方案。从性能角度和考虑到可能使用indexed_search,如果页面的剩余部分被缓存,那么效果也更好。 - Ben

0
感谢TYPO3UA的回答。但是你应该使用'afterBuildingFinished'钩子,因为'initializeFormElement'钩子在表单元素中设置表单定义属性之前执行。因此,表单定义中的默认值(即使它是空字符串)将覆盖在'initializeFormElement'钩子中设置的值。 请参见:https://forge.typo3.org/issues/82615 因此,这适用于设置表单元素的默认值:
/**
* @param \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable
* @return void
*/
public function afterBuildingFinished(\TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable)
{        
        if (method_exists($renderable, 'getUniqueIdentifier') && $renderable->getUniqueIdentifier() === 'contactForm-text-1') {
            $renderable->setDefaultValue('Value');
        }
}

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterBuildingFinished'][<useATimestampAsKeyPlease>]
    = \VENDOR\YourNamespace\YourClass::class;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接