Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: cleanup form output #34

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Classes/Form/AbstractModelDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace Remind\Headless\Form;

use FriendsOfTYPO3\Headless\Form\Decorator\AbstractFormDefinitionDecorator;
use Psr\Http\Message\ServerRequestInterface;

abstract class AbstractModelDecorator extends AbstractFormDefinitionDecorator
abstract class AbstractModelDecorator extends FormDefinitionDecorator
{
protected string $actionName = '';
protected string $controllerName = '';
protected string $valueName = '';

protected function overrideDefinition(array $decorated, array $definition, int $currentPage): array
{
$decorated = parent::overrideDefinition($decorated, $definition, $currentPage);

$request = $this->getRequest();

/** @var \TYPO3\CMS\Core\Routing\PageArguments $pageArguments */
Expand Down
67 changes: 67 additions & 0 deletions Classes/Form/FormDefinitionDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Remind\Headless\Form;

use FriendsOfTYPO3\Headless\Form\Decorator\AbstractFormDefinitionDecorator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

class FormDefinitionDecorator extends AbstractFormDefinitionDecorator
{
private const NOT_EMPTY_ERROR_CODES = [
1221560910,
1221560718,
1347992400,
1347992453,
];

private ContentObjectRenderer $cObj;
public function __construct(array $formStatus = [])
{
parent::__construct($formStatus);
$this->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}

protected function overrideDefinition(array $decorated, array $definition, int $currentPage): array
{
foreach ($decorated['elements'] as &$element) {
$this->setNotEmptyValidationErrorMessages($element);
$this->setCheckboxLinks($element);
}
return $decorated;
}

private function setNotEmptyValidationErrorMessages(array &$element): void
{
$notEmptyValidators = array_filter($element['validators'] ?? [], function (array $validator) {
return $validator['identifier'] === 'NotEmpty';
});
if ($notEmptyValidators) {
foreach ($element['properties']['validationErrorMessages'] ?? [] as $validationErrorMessageKey => $validationErrorMessage) {
if (in_array($validationErrorMessage['code'], self::NOT_EMPTY_ERROR_CODES)) {
foreach (array_keys($notEmptyValidators) as $validatorKey) {
$element['validators'][$validatorKey]['customErrorMessage'] = $validationErrorMessage['customMessage'];
}
unset($element['properties']['validationErrorMessages'][$validationErrorMessageKey]);
}
}
if (empty($element['properties']['validationErrorMessages'])) {
unset($element['properties']['validationErrorMessages']);
}
}
unset($element['properties']['fluidAdditionalAttributes']);
}

private function setCheckboxLinks(array &$element): void
{
if ($element['type'] === 'Checkbox' && isset($element['properties']['links'])) {
foreach ($element['properties']['links'] as $pageUid => $label) {
$link = $this->cObj->createLink($label, ['parameter' => $pageUid])->getHtml();
$element['label'] = sprintf($element['label'], $link);
}
unset($element['properties']['links']);
}
}
}
27 changes: 26 additions & 1 deletion Configuration/Form/BaseSetup.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
imports:
- { resource: './FormElements/AdvancedPassword.yaml' }
- { resource: './FormElements/Checkbox.yaml' }
- { resource: './FormElements/Date.yaml' }
- { resource: './FormElements/Email.yaml' }
- { resource: './FormElements/GridRow.yaml' }
- { resource: './FormElements/Fieldset.yaml' }
- { resource: './FormElements/MultiCheckbox.yaml' }
- { resource: './FormElements/MultiSelect.yaml' }
- { resource: './FormElements/Number.yaml' }
- { resource: './FormElements/Password.yaml' }
- { resource: './FormElements/RadioButton.yaml' }
- { resource: './FormElements/SingleSelect.yaml' }
- { resource: './FormElements/StaticText.yaml' }
- { resource: './FormElements/Telephone.yaml' }
- { resource: './FormElements/Text.yaml' }
- { resource: './FormElements/Textarea.yaml' }
- { resource: './FormElements/Url.yaml' }

- { resource: './Finishers/JsonRedirectFinisher.yaml' }

TYPO3:
CMS:
Form:
Expand All @@ -10,12 +31,16 @@ TYPO3:
translationFiles:
20: "EXT:rmnd_headless/Resources/Private/Language/locallang_form.xlf"
formElementsDefinition:
DatePicker: null
DatePicker:
formEditor:
group: null
Form:
formEditor:
predefinedDefaults:
i18n:
identifier: i18n
renderingOptions:
formDecorator: Remind\Headless\Form\FormDefinitionDecorator
editors:
# Overwrite submitButtonLabel propertyPath to be included in json output
300:
Expand Down
75 changes: 0 additions & 75 deletions Configuration/Form/FormElementSize.yaml

This file was deleted.

95 changes: 95 additions & 0 deletions Configuration/Form/FormElements/AdvancedPassword.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
TYPO3:
CMS:
Form:
prototypes:
standard:
formElementsDefinition:
AdvancedPassword:
formEditor:
editors:
400:
propertyPath: properties.placeholder
# Remove default Grid viewport configuration
700: null
701:
identifier: size
templateName: Inspector-SingleSelectEditor
label: formEditor.elements.FormElement.editor.size.label
propertyPath: properties.size
selectOptions:
10:
value: ""
label: formEditor.elements.FormElement.editor.size.default.label
20:
value: xs
label: formEditor.elements.FormElement.editor.size.xs.label
30:
value: sm
label: formEditor.elements.FormElement.editor.size.sm.label
40:
value: md
label: formEditor.elements.FormElement.editor.size.md.label
50:
value: lg
label: formEditor.elements.FormElement.editor.size.lg.label
60:
value: xl
label: formEditor.elements.FormElement.editor.size.xl.label
propertyCollections:
validators:
10:
editors:
200:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
20:
editors:
200:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
30:
editors:
200:
additionalElementPropertyPaths: null
300:
additionalElementPropertyPaths: null
400:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
40:
editors:
200:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
50:
editors:
200:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
60:
editors:
200:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
70:
editors:
200:
additionalElementPropertyPaths: null
300:
additionalElementPropertyPaths: null
400:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
80:
editors:
300:
templateName: Inspector-TextEditor
propertyPath: customErrorMessage
doNotSetIfPropertyValueIsEmpty: true
49 changes: 49 additions & 0 deletions Configuration/Form/FormElements/Checkbox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
TYPO3:
CMS:
Form:
prototypes:
standard:
formElementsDefinition:
Checkbox:
formEditor:
editors:
300:
identifier: links
templateName: Inspector-PropertyGridEditor
label: formEditor.elements.FormElement.editor.links.label
propertyPath: properties.links
isSortable: true
enableAddRow: true
enableDeleteRow: true
useLabelAsFallbackValue: false
gridColumns:
- name: label
title: formEditor.elements.FormElement.editor.links.linkText
- name: value
title: formEditor.elements.FormElement.editor.links.pageUid
# Remove default Grid viewport configuration
700: null
701:
identifier: size
templateName: Inspector-SingleSelectEditor
label: formEditor.elements.FormElement.editor.size.label
propertyPath: properties.size
selectOptions:
10:
value: ""
label: formEditor.elements.FormElement.editor.size.default.label
20:
value: xs
label: formEditor.elements.FormElement.editor.size.xs.label
30:
value: sm
label: formEditor.elements.FormElement.editor.size.sm.label
40:
value: md
label: formEditor.elements.FormElement.editor.size.md.label
50:
value: lg
label: formEditor.elements.FormElement.editor.size.lg.label
60:
value: xl
label: formEditor.elements.FormElement.editor.size.xl.label
Loading