david123456789
david123456789

Reputation: 43

Typo3 9.5 prefill form field with get parameter

I use TYPO3 system extension "form" and want to prefill an input field with a GET parameter.

This TYPO3 8.7. Form prefill input field is working, but only is no_cache=1. Is there another solution without deactivate the whole cache?

Thanks david

Upvotes: 3

Views: 2625

Answers (3)

Fabian König
Fabian König

Reputation: 11

Thanks TYPO3UA for you answer. But you should use the hook 'afterBuildingFinished' because the 'initializeFormElement' hook is executed BEFORE the properties from the form definition are set in the form element. So the default values from the form definition (even it is an empty string) will override the values set in the initializeFormElement' hook. See: https://forge.typo3.org/issues/82615

So this works for setting the default value of an form element:

/**
* @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;

Upvotes: 1

Ben
Ben

Reputation: 1031

You can disable the cache of the content column of your form page, for example:

lib.content = COA
lib.content{
    10 < styles.content.get
}
[page["uid"] == 512]
    lib.content = COA_INT
[global]

Upvotes: 0

TYPO3UA
TYPO3UA

Reputation: 89

Yes, you can but you need to create HOOK.

This is described in the documentation

For example, the HOOK

/**
 * @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');
    }
}

And the connect the hook

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

Please, read the documentation for "Form framework".

I did it and get results what I need.

Upvotes: 1

Related Questions