Issue Feature Rejected

#92402FlexFormService should convert flexform values into their correct data types to use in JSON APIs

Bastian Stargazer 09/24/20, 18:29 updated 06/06/24, 11:09

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.

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.