Skip to content

Commit

Permalink
[TASK] Improve form support with v12 (#554)
Browse files Browse the repository at this point in the history
Fix fe-login flashMessages partial with fe debug enabled (#546)
Submitted form should return sent data to the frontend (#547)
Also resolves #530
* [TASK] Improve form support with v12
resolves #550
  • Loading branch information
twoldanski authored Feb 23, 2023
1 parent 8097240 commit b433df0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
12 changes: 7 additions & 5 deletions Classes/Form/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(FormTranslationService $service = null)
* @param array<mixed> $renderingOptions
* @return array<mixed>
*/
public function translate(array $formDefinition, array $renderingOptions): array
public function translate(array $formDefinition, array $renderingOptions, array $sentValues = []): array
{
$result['renderables'] = [];
$formRuntime = [
Expand All @@ -57,7 +57,7 @@ public function translate(array $formDefinition, array $renderingOptions): array
continue;
}

$pageTranslation['renderables'] = $this->translateRenderables($page['renderables'], $formRuntime);
$pageTranslation['renderables'] = $this->translateRenderables($page['renderables'], $formRuntime, $sentValues);

$result['renderables'][] = array_replace_recursive($page, $pageTranslation);
}
Expand All @@ -68,15 +68,16 @@ public function translate(array $formDefinition, array $renderingOptions): array
/**
* @param array<int, mixed> $renderables
* @param array<string, mixed> $formRuntime
* @param array<string, mixed> sentValues
* @return array<int, mixed>
*/
private function translateRenderables(array $renderables, array $formRuntime): array
private function translateRenderables(array $renderables, array $formRuntime, array $sentValues): array
{
foreach ($renderables as &$element) {
$properties = [];

if (isset($element['renderables']) && is_array($element['renderables'])) {
$element['renderables'] = $this->translateRenderables($element['renderables'], $formRuntime);
$element['renderables'] = $this->translateRenderables($element['renderables'], $formRuntime, $sentValues);
}

if (isset($element['validators']) &&
Expand Down Expand Up @@ -133,7 +134,8 @@ private function translateRenderables(array $renderables, array $formRuntime): a
$formRuntime
);

$element['defaultValue'] = $translatedDefaultValue ?: ($element['defaultValue'] ?? '');
$element['defaultValue'] = $translatedDefaultValue !== '' && $translatedDefaultValue !== null ? $translatedDefaultValue : ($element['defaultValue'] ?? '');
$element['value'] = $sentValues[$element['identifier']] ?? null;
$element['properties'] = $properties;
}

Expand Down
10 changes: 5 additions & 5 deletions Classes/XClass/Controller/FormFrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function renderAction(): ResponseInterface
if (!empty($this->settings['persistenceIdentifier'])) {
$formDefinition = $this->formPersistenceManager->load($this->settings['persistenceIdentifier']);
$formDefinition['persistenceIdentifier'] = $this->settings['persistenceIdentifier'];
$formDefinition = $this->overrideByTypoScriptSettings($formDefinition);
$formDefinition = $this->overrideByFlexFormSettings($formDefinition);
$formDefinition = ArrayUtility::setValueByPath(
$formDefinition,
Expand Down Expand Up @@ -119,7 +118,7 @@ public function renderAction(): ResponseInterface
/**
* @var FormRuntime $formRuntime
*/
$formRuntime = $formDefinitionObj->bind($this->controllerContext->getRequest());
$formRuntime = $formDefinitionObj->bind($this->request);
$formState = $formRuntime->getFormState();
$finisherResponse = $formRuntime->run();

Expand All @@ -135,7 +134,7 @@ public function renderAction(): ResponseInterface

$currentPageIndex = $formRuntime->getCurrentPage() ? $formRuntime->getCurrentPage()->getIndex() : 0;
$currentPageId = $currentPageIndex + 1;
$formFields = $formDefinition['renderables'][$currentPageIndex]['renderables'];
$formFields = $formDefinition['renderables'][$currentPageIndex]['renderables'] ?? [];

// provides support for custom options providers (dynamic selects/radio/checkboxes)
$formFieldsNames = $this->generateFieldNamesAndReplaceCustomOptions($formFields, $formDefinition['identifier'], $formRuntime->getFormDefinition());
Expand Down Expand Up @@ -193,7 +192,8 @@ public function renderAction(): ResponseInterface
$formDefinition['i18n'] = count($i18n) ? $i18n : null;
$formDefinition = $this->jsonFormTranslator->translate(
$formDefinition,
$formRuntime->getFormDefinition()->getRenderingOptions()
$formRuntime->getFormDefinition()->getRenderingOptions(),
$formRuntime->getFormState() ? $formRuntime->getFormState()->getFormValues() : []
);

$formStatus['status'] = null;
Expand All @@ -207,7 +207,7 @@ public function renderAction(): ResponseInterface

if ($formState &&
$formState->isFormSubmitted() &&
$this->getControllerContext()->getRequest()->getMethod() === 'POST') {
$this->request->getMethod() === 'POST') {
$result = $formRuntime->getRequest()->getOriginalRequestMappingResults();
/**
* @var array<string, Error[]>
Expand Down
4 changes: 4 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use FriendsOfTYPO3\Headless\ContentObject\JsonContentContentObject;
use FriendsOfTYPO3\Headless\ContentObject\JsonContentObject;
use FriendsOfTYPO3\Headless\DataProcessing\MenuProcessor;
use FriendsOfTYPO3\Headless\Form\Service\FormTranslationService;
use FriendsOfTYPO3\Headless\Seo\XmlSitemap\XmlSitemapRenderer;
use FriendsOfTYPO3\Headless\Utility\HeadlessFrontendUrlInterface;
use FriendsOfTYPO3\Headless\Utility\UrlUtility;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\CMS\Form\Controller\FormFrontendController;
use TYPO3\CMS\FrontendLogin\Controller\LoginController;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $configurator): void {
$services = $configurator->services()
->defaults()
Expand Down Expand Up @@ -59,4 +62,5 @@

$services->set(HeadlessFrontendUrlInterface::class, UrlUtility::class)->autowire(false);
$services->set(XmlSitemapRenderer::class)->public()->share(false);
$services->set(FormTranslationService::class)->arg('$runtimeCache', service('cache.runtime'))->public();
};
26 changes: 14 additions & 12 deletions Resources/Private/FeLogin/Partials/FlashMessages.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<f:spaceless>
<f:format.raw>
<f:flashMessages as="flashMessages">
[
<f:for each="{flashMessages}" as="flashMessage" iteration="msgIterator">
<f:format.json value="{
<f:variable name="result">[]</f:variable>
<f:flashMessages as="flashMessages">
<f:variable name="result">
[
<f:for each="{flashMessages}" as="flashMessage" iteration="msgIterator">
<f:format.json value="{
title: '{flashMessage.title}',
message: '{flashMessage.message}',
severity: '{flashMessage.severity}',
className: '{f:render(section:\'errorClass\', arguments:\'{severity: flashMessage.severity}\')}'
}"/>
{f:if(condition: msgIterator.isLast, else: ',')}
</f:for>
]
</f:flashMessages>
</f:format.raw>
}" />
{f:if(condition: msgIterator.isLast, else: ',')}
</f:for>
]
</f:variable>
</f:flashMessages>
<f:spaceless>
<f:format.raw>{result}</f:format.raw>
</f:spaceless>
<f:section name="errorClass">
<f:spaceless>
Expand Down
28 changes: 21 additions & 7 deletions Tests/Unit/Form/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function test(): void
'identifier' => 'testfield',
'label' => 'test field',
'properties' => [],
'value' => null,
],
[
'type' => 'input',
Expand All @@ -47,6 +48,7 @@ public function test(): void
['code' => 111, 'message' => 'translateMe'],
]
],
'value' => null,
],
[
'type' => 'Fieldset',
Expand All @@ -61,6 +63,7 @@ public function test(): void
'properties' => [],
],
],
'value' => null,
],
[
'type' => 'input',
Expand All @@ -80,19 +83,22 @@ public function test(): void
'FERegularExpression' => '/a-z/',
]
],
'value' => null,
],
[
'type' => 'input',
'identifier' => 'overridden',
'label' => 'overridden field',
'properties' => [],
'renderingOptions' => ['FEOverrideType' => 'select'],
'value' => null,
],
[
'type' => 'ImageUpload',
'identifier' => 'image',
'label' => 'Upload image',
'properties' => ['saveToFileMount' => '/upload-dir'],
'value' => null,
]
]
],
Expand All @@ -112,7 +118,8 @@ public function test(): void
'identifier' => 'testfield',
'label' => 'translatedValue',
'properties' => [],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'input',
Expand All @@ -123,7 +130,8 @@ public function test(): void
['code' => 111, 'message' => 'translatedError'],
]
],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'Fieldset',
Expand All @@ -136,10 +144,12 @@ public function test(): void
'identifier' => 'nested',
'label' => 'translatedValue',
'properties' => [],
'defaultValue' => 'translatedValue'
'defaultValue' => 'translatedValue',
'value' => null,
],
],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'input',
Expand All @@ -159,25 +169,29 @@ public function test(): void
'FERegularExpression' => '/a-z/',
]
],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'input',
'identifier' => 'overridden',
'label' => 'translatedValue',
'properties' => [],
'renderingOptions' => ['FEOverrideType' => 'select'],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'ImageUpload',
'identifier' => 'image',
'label' => 'translatedValue',
'properties' => ['saveToFileMount' => 'translatedValue'],
'value' => null,
'defaultValue' => 'translatedValue',
]
],
'label' => 'translatedValue'
'label' => 'translatedValue',

],
]
], $translator->translate($formDefinition, []));
Expand Down

0 comments on commit b433df0

Please sign in to comment.