AfterFormStateInitializedEvent
New in version 15.0 Feature: #109811 - PSR-14 event AfterFormStateInitializedEvent
The event replaces the removed hook
$GLOBALS
and the interface
\TYPO3\.
The event
\TYPO3\ is
dispatched after the form runtime has restored the form state from the
request. The submitted values and the form definition are both available.
A listener can read and change the form state through $form and
read the request in $request.
Example: Fill a form field from a query parameter
The following listener writes the query parameter campaign into the form
field campaign, unless the field already has a value. The form then sends
the campaign that the visitor came from along with the other values:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Form\Event\AfterFormStateInitializedEvent;
final readonly class CampaignFromQueryListener
{
#[AsEventListener(
identifier: 'my-extension/campaign-from-query',
)]
public function __invoke(AfterFormStateInitializedEvent $event): void
{
$formState = $event->formRuntime->getFormState();
if ($formState === null) {
return;
}
// Remember the campaign the visitor came from, unless already set
$campaign = $event->request->getQueryParams()['campaign'] ?? '';
if ($campaign !== '' && $formState->getFormValue('campaign') === null) {
$formState->setFormValue('campaign', $campaign);
}
}
}