Skip to content

Commit

Permalink
feat: add links to form checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdmlln committed Jul 23, 2024
1 parent ecb7fe3 commit af27e1f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
57 changes: 42 additions & 15 deletions Classes/Form/FormDefinitionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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
{
Expand All @@ -14,27 +16,52 @@ class FormDefinitionDecorator extends AbstractFormDefinitionDecorator
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) {
$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]);
$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']);
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']);
}
return $decorated;
}
}
14 changes: 14 additions & 0 deletions Configuration/Form/FormElements/Checkbox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ TYPO3:
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:
Expand Down
9 changes: 9 additions & 0 deletions Resources/Private/Language/de.locallang_form.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
<trans-unit id="formEditor.elements.Form.editor.requiredHint.label">
<target>Pflichtfeld Hinweis</target>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.links.label">
<target>Links</target>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.links.linkText">
<target>Link Text</target>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.links.pageUid">
<target>Seite</target>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.size.label">
<target>Größe</target>
</trans-unit>
Expand Down
9 changes: 9 additions & 0 deletions Resources/Private/Language/locallang_form.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
<trans-unit id="formEditor.elements.Form.editor.requiredHint.label">
<source>Required field hint</source>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.links.label">
<source>Links</source>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.links.linkText">
<source>Link Text</source>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.links.pageUid">
<source>Page</source>
</trans-unit>
<trans-unit id="formEditor.elements.FormElement.editor.size.label">
<source>Size</source>
</trans-unit>
Expand Down

0 comments on commit af27e1f

Please sign in to comment.