AfterFormStateInitializedEvent 

New in version 15.0 Feature: #109811 - PSR-14 event AfterFormStateInitializedEvent

The event replaces the removed hook $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterFormStateInitialized'] and the interface \TYPO3\CMS\Form\Domain\Runtime\FormRuntime\Lifecycle\AfterFormStateInitializedInterface .

The event \TYPO3\CMS\Form\Event\AfterFormStateInitializedEvent 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 $formRuntime 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:

EXT:my_extension/Classes/EventListener/CampaignFromQueryListener.php
<?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);
    }
  }
}
Copied!

API 

final class AfterFormStateInitializedEvent
Fully qualified name
\TYPO3\CMS\Form\Event\AfterFormStateInitializedEvent

Listeners to this Event will be able to modify the form runtime after it has been initialized.

public formRuntime
public readonly request