Skip to content

Instantly share code, notes, and snippets.

@thommyhh
Created September 1, 2021 17:00
Show Gist options
  • Save thommyhh/bd73e9e18272e798c22c1a4f9c012f99 to your computer and use it in GitHub Desktop.
Save thommyhh/bd73e9e18272e798c22c1a4f9c012f99 to your computer and use it in GitHub Desktop.
TYPO3 EXT:form afterSubmit hook per form definition
<?php
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'][\YourVendor\YourPackage\Form\StatisticsHook::class] = \YourVendor\YourPackage\Form\StatisticsHook::class;
<?php
declare(strict_types=1);
namespace YourVendor\YourPackage\Form;
use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable;
use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
class StatisticsHook
{
/**
* @param FormRuntime $formRuntime
* @param AbstractRenderable $renderable
* @param string|int $elementValue
* @param array<string|int> $requestArguments
*
* @return string|int
*/
public function afterSubmit(FormRuntime $formRuntime, RenderableInterface $renderable, $elementValue, array $requestArguments = [])
{
// The hook is not called for the form itself but for all children, therefore check for the page and get the parent renderable (the form)
if ($renderable->getType() === 'Page' && !empty($renderable->getParentRenderable()->getRenderingOptions()['statistics'])) {
$statisticSettings = $renderable->getParentRenderable()->getRenderingOptions()['statistics'];
if ($statisticSettings['enable']) {
// ... do what ever you like
// Use `$requestArguments` to get the submitted values of the current step/page, as they are not processed yet
}
}
// You must return the element value in any case, otherwise the submitted value is lost
return $elementValue;
}
}
renderingOptions:
submitButtonLabel: Submit
statistics:
enable: true
translation:
translationFiles:
100: '...'
fluidAdditionalAttributes:
novalidate:
type: Form
identifier: yourFormIdentifier
label: 'The form name or label'
prototypeName: standard
finishers:
...
renderables:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment