Reputation: 2243
I have extended and adjusted the flexform of ext:form
like described here:
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']
['SC_OPTIONS']
[\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools::class]
['flexParsing']
[] = \Vendor\ExtKey\Hooks\FlexFormHook::class;
Classes/Hooks/FlexFormHook.php:
<?php
namespace Vendor\ExtKey\Hooks;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class FlexFormHook
{
/**
* @param array $dataStructure
* @param array $identifier
* @return array
*/
public function parseDataStructureByIdentifierPostProcess(array $dataStructure, array $identifier): array
{
if ($identifier['type'] === 'tca' && $identifier['tableName'] === 'tt_content' && $identifier['dataStructureKey'] === '*,form_formframework') {
$file = Environment::getPublicPath() . '/typo3conf/ext/extKey/Configuration/Example.xml';
$content = file_get_contents($file);
if ($content) {
$dataStructure['sheets']['extraEntry'] = GeneralUtility::xml2array($content);
}
}
return $dataStructure;
}
}
Flexform:
<extra>
<ROOT>
<TCEforms>
<sheetTitle>Fo</sheetTitle>
</TCEforms>
<type>array</type>
<el>
<settings.postsPerPage>
<TCEforms>
<label>Max. number of posts to display per page</label>
<config>
<type>input</type>
<size>2</size>
<eval>int</eval>
<default>3</default>
</config>
</TCEforms>
</settings.postsPerPage>
</el>
</ROOT>
</extra>
Now I am able to set custom settings for a form and there is no need to double the YAML-configuration of the form, so far the theory.
BUT:
I can not access the settings with {settings.postsPerPage}
in the templates nor I am able to get them in a custom finisher.
Is it somehow possible to get my custom settings in the template of ext:form
and in my custom finisher? If so, how?
Upvotes: 1
Views: 1102
Reputation: 1476
As a rule of thumb: there are so much ways in form framework, that it is hard to understand a single one :-)
Fact: $this->settings is assigned to the view, but is not passed to the formvh:render viewhelper. So you can't access it in the templates. You have to resolve the flexform (no matter if extended or not) yourself and assign it to your form configuration.
One way of solving this would be:
1. Hook into afterInitializeCurrentPage process
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'][1614844400]
= \Vendor\Extension\Hooks\FormHooks::class;
2. Read your flexform and assign your variables as e.g. renderingOptions of the form definition:
\Vendor\Extension\Hooks\FormHooks:
public function afterInitializeCurrentPage(
\TYPO3\CMS\Form\Domain\Runtime\FormRuntime $formRuntime,
\TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface $currentPage = null,
\TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface $lastPage = null,
array $requestArguments = []): ?\TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
{
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
$flexFormData = GeneralUtility::xml2array($configurationManager->getContentObject()->data['pi_flexform'] ?? '');
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
$flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
$sheetDataXml = $flexFormTools->flexArray2Xml($flexFormData);
$settings = $flexFormService->convertFlexFormContentToArray($sheetDataXml)['settings'] ?? [];
if (array_key_exists('postsPerPage', $settings)) {
$formRuntime->getFormDefinition()->setRenderingOption('postsPerPage', $settings['postsPerPage']);
}
return $currentPage;
}
3. Access your settings in your templates or in your finisher:
Fluid:
<formvh:renderRenderable renderable="{form}">
<f:debug>{form.formDefinition.renderingOptions.postsPerPage}</f:debug>
</formvh:renderRenderable>
Finisher:
$this->finisherContext->getFormRuntime()->getFormDefinition()->getRenderingOptions()
This is a quick and dirty but working code example. Don't use it in production without clean, lint and finish :-)
Possible other ways:
Upvotes: 2