Feature #92402
closedFlexFormService should convert flexform values into their correct data types to use in JSON APIs
0%
Description
The FlexFormService should convert values into their correct data types, at least for the base types like numbers (Integer/Float). The background behind this is the use of the coming FlexFormProcessor (see feature #89509) for JSON APIs like EXT:headless or EXT:t3api.
At the moment using this processor on the following Flexform structure
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3FlexForms>
<data>
<sheet index="sDEF">
<language index="lDEF">
<field index="repetitionCount">
<value index="vDEF">10.7</value>
</field>
<field index="pauseInSeconds">
<value index="vDEF">5</value>
</field>
<field index="allowSwitch">
<value index="vDEF">1</value>
</field>
</language>
</sheet>
</data>
</T3FlexForms>
will result in JSON as
"flexform": {
"average": "10.7",
"pauseInSeconds": "5",
"allowSwitch": "1"
}
Adding the following function would be a great improvement:
public function walkFlexFormNode($nodeArray, $valuePointer = 'vDEF') {
// ...
return $this->convertDataType($nodeValue); // changed line 84 (maybe also line 109?)
// ...
}
// function to convert numeric values into int or float/double
private function convertDataType($value)
{
if (is_numeric($value)) {
if(FALSE !== strpos($value, '.')) {
return floatval($value);
} else {
return intval($value);
}
}
return $value;
}
Results in
"flexform": {
"average": 10.7000000001,
"pauseInSeconds": 5,
"allowSwitch": 1
}
The key "allowSwitch" with values of 0/1 is evaluated as boolean in JavaScript much easier this way.
Attached is the updated FlexFormService.php file.
Files
Updated by Georg Ringer about 4 years ago
- Related to Feature #89509: Data Processor to resolve flexform data added
Updated by Georg Ringer about 4 years ago
- Related to Feature #82212: TYPO3 should be able to store JSON instead of XML in FlexForm fields added
Updated by Georg Ringer about 4 years ago
- Status changed from New to Needs Feedback
I would be in favor of waiting for #82212 as this would have native json in the DB instead. what do you think?
Updated by Bastian Stargazer about 4 years ago
Georg Ringer wrote:
I would be in favor of waiting for #82212 as this would have native json in the DB instead. what do you think?
Oh yes please, that's even better! Can't wait to see this implemented in core!
On the other hand it might be still useful to convert types also for existing Flexform values, for backward compatibility (I mean to use existing extensions in JSON APIs) and for the time until the new feature #82212 is used widhly.
Updated by Bastian Stargazer over 2 years ago
Since #82212 was rejected, here is another proposal to improve the current Flexform solution which probably will not break backward compatibility. How about to enrich the current structure with "type" information for each field? E.g.:
<T3FlexForms>
<data>
<sheet index="sDEF">
<language index="lDEF">
<field index="repetitionCount" type="float">
<value index="vDEF">10.7</value>
</field>
<field index="pauseInSeconds" type="int">
<value index="vDEF">5</value>
</field>
<field index="allowSwitch" type="boolean">
<value index="vDEF">1</value>
</field>
</language>
</sheet>
</data>
</T3FlexForms>
The type could be specified manually inside the Flexform configuration. If no "type" attribute is given (legacy), each value is returned as "string" for backward compatibility.
In this way, the FlexformProcessor could resolve an array with values in their correct data types, which would allow us to use config -> f.format.json() to build a clean usable(!) JSON structure inside Fluid, e.g. to pass as data-attribute to JavaScript.
Updated by Georg Ringer 6 months ago
- Status changed from Needs Feedback to Rejected
thanks for the detailed comment. Flexforms won't get more attention and adding stuff which is not available in e.g. TCA.
If you need to strict types (Which is of course a good thing), please run it through some code in your controller/service/... and validate it there.