Project

General

Profile

Actions

Feature #92402

open

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

Added by Bastian Stargazer over 3 years ago. Updated almost 2 years ago.

Status:
Needs Feedback
Priority:
Should have
Assignee:
-
Category:
-
Target version:
Start date:
2020-09-24
Due date:
% Done:

0%

Estimated time:
PHP Version:
Tags:
flexformservice,flexformprocessor,api,json
Complexity:
easy
Sprint Focus:

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

FlexFormService.php (4.98 KB) FlexFormService.php Updated FlexFormService Bastian Stargazer, 2020-09-24 18:24

Related issues 2 (0 open2 closed)

Related to TYPO3 Core - Feature #89509: Data Processor to resolve flexform dataClosedJo Hasenau2019-10-25

Actions
Related to TYPO3 Core - Feature #82212: TYPO3 should be able to store JSON instead of XML in FlexForm fieldsRejected2017-08-26

Actions
Actions

Also available in: Atom PDF