Skip to content

Commit

Permalink
[FEATURE] Add option to skip saving composite fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Feustel committed Sep 4, 2024
1 parent f1df924 commit bfe8f2b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
30 changes: 28 additions & 2 deletions Classes/Domain/Form/Finishers/LoggerFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use TYPO3\CMS\Extbase\Domain\Model\FileReference as ExtbaseFileReference;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
use TYPO3\CMS\Form\Domain\Model\FormElements\StringableFormElementInterface;

/**
Expand All @@ -32,6 +33,8 @@ class LoggerFinisher extends AbstractFinisher implements LoggerAwareInterface
*/
protected $defaultOptions = [
'finisherVariables' => [],
'includeHiddenElements' => true,
'skipElementsTypes' => 'ContentElement,StaticText',
];

/**
Expand Down Expand Up @@ -92,8 +95,33 @@ protected function getFormValues(): array
{
$normalizedFormValues = [];
$formDefinition = $this->finisherContext->getFormRuntime()->getFormDefinition();
$skipHiddenElements = !$this->parseOption('includeHiddenElements');
$skipElementTypes = GeneralUtility::trimExplode(',', $this->parseOption('skipElementsTypes'));

foreach ($this->finisherContext->getFormValues() as $identifier => $formValue) {

$element = $formDefinition->getElementByIdentifier($identifier);
$elementType = null;
if ($element !== null) {
$renderingOptions = $element->getRenderingOptions();
$elementType = $element->getType();
}
if (
$skipHiddenElements &&
(
// Mimik the logik of \TYPO3\CMS\Form\Domain\Finishers\EmailFinisher
// with conditions against {formValue.value} and {formValue.isSection} in
// EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/Default.html
// where {formValue.isSection} is set in \TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper::renderStatic()
($renderingOptions['_isCompositeFormElement'] ?? false)
|| ($renderingOptions['_isSection'] ?? false)
// additionally skip configurable element types which don't actually have form values (e.g. StaticText)
|| in_array($elementType, $skipElementTypes)
)
) {
continue;
}

if (is_object($formValue)) {
if ($formValue instanceof ExtbaseFileReference) {
$formValue = $formValue->getOriginalResource();
Expand All @@ -108,8 +136,6 @@ protected function getFormValues(): array
continue;
}

$element = $formDefinition->getElementByIdentifier($identifier);

if ($element instanceof StringableFormElementInterface) {
$normalizedFormValues[$identifier] = $element->valueToString($formValue);
continue;
Expand Down
9 changes: 9 additions & 0 deletions Configuration/Form/Setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ TYPO3:
identifier: header
templateName: Inspector-CollectionElementHeaderEditor
label: formEditor.elements.Form.editor.finishers.LogFormData.label
200:
identifier: 'includeHiddenElements'
templateName: 'Inspector-CheckboxEditor'
label: 'formEditor.elements.Form.editor.finishers.LogFormData.includeHiddenElements.label'
propertyPath: 'options.includeHiddenElements'
9999:
identifier: removeButton
templateName: Inspector-RemoveElementEditor
Expand All @@ -32,3 +37,7 @@ TYPO3:
formEditor:
iconIdentifier: form-finisher
label: formEditor.elements.Form.editor.finishers.LogFormData.label
predefinedDefaults:
options:
includeHiddenElements: true
skipElementsTypes: 'ContentElement,StaticText'
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ finishers:

The `LogFormData` finisher should be the last finisher or right before the `Redirect` finisher if used. Logging after a redirect is not possible.

## Configuration

### Save values from hidden or composite Elements

Some form elements, such as fieldsets or other containers, do not hold a form value by themselves; only their child elements hold values. By default, all form elements are saved, even those that never contain values, which can result in empty columns in the log records and exports.

To prevent this, set the finisher option `includeHiddenElements` to `false`. This option ensures that values from composite elements are not saved, similar to how the `EmailFinisher` only includes fields in emails that actually contain a value.

Additionally, you can use the finisher option `skipElementsTypes` to exclude specific form elements (comma separated list). By default, this option excludes `ContentElement` and `StaticText` elements, as they will never have a value.

### Logging of finisher variables

Additional variables stored in the `FinisherVariableProvider` can also be logged by using the `finisherVariables` option:

```
Expand Down
4 changes: 3 additions & 1 deletion Resources/Private/Language/Database.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<trans-unit id="formEditor.elements.Form.editor.finishers.LogFormData.label">
<source>Log form data</source>
</trans-unit>

<trans-unit id="formEditor.elements.Form.editor.finishers.LogFormData.includeHiddenElements.label">
<source>Store composite form element data?</source>
</trans-unit>
</body>
</file>
</xliff>
5 changes: 4 additions & 1 deletion Resources/Private/Language/de.Database.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
<source>Log form data</source>
<target>Formulardaten loggen</target>
</trans-unit>

<trans-unit id="formEditor.elements.Form.editor.finishers.LogFormData.includeHiddenElements.label">
<source>Store composite form element data?</source>
<target>Formulardaten von Composite-Elementen speichern?</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit bfe8f2b

Please sign in to comment.