Created
March 27, 2023 07:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sebkln\FormExamples\Hooks\PrefillFormWithCurrentPageHook; | |
defined('TYPO3') or die(); | |
(static function () { | |
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'][1679897346] = PrefillFormWithCurrentPageHook::class; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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