Skip to content

Instantly share code, notes, and snippets.

@sebkln
Created March 27, 2023 07:54
Show Gist options
  • Save sebkln/4a3c88b03aa0a72dd6ee5ee3ad8c0ac2 to your computer and use it in GitHub Desktop.
Save sebkln/4a3c88b03aa0a72dd6ee5ee3ad8c0ac2 to your computer and use it in GitHub Desktop.
TYPO3 Form Framework: Use hook to prefill hidden form field with current page title and UID
<?php
use Sebkln\FormExamples\Hooks\PrefillFormWithCurrentPageHook;
defined('TYPO3') or die();
(static function () {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'][1679897346] = PrefillFormWithCurrentPageHook::class;
})();
<?php
namespace Sebkln\FormExamples\Hooks;
use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
class PrefillFormWithCurrentPageHook {
/**
* @param FormRuntime $formRuntime
* @param RootRenderableInterface $renderable
* @return void
*
* Usage in form definition:
* -
* defaultValue: ''
* type: Hidden
* identifier: pageInfo
* label: 'Current page'
* variants:
* -
* identifier: hide-pageInfo
* renderingOptions:
* enabled: false
* condition: 'stepType == "SummaryPage" || finisherIdentifier in ["EmailToSender"]'
*/
public function beforeRendering(FormRuntime $formRuntime, RootRenderableInterface $renderable): void
{
if ($renderable->getIdentifier() === 'pageInfo') {
$pageTitle = $GLOBALS['TSFE']->page['title'];
$pageId = $GLOBALS['TSFE']->id;
$renderable->setDefaultValue($pageTitle . ' (ID: ' . $pageId . ')');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment