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 #1

Updated by Georg Ringer over 3 years ago

  • Related to Feature #89509: Data Processor to resolve flexform data added
Actions #2

Updated by Georg Ringer over 3 years ago

  • Related to Feature #82212: TYPO3 should be able to store JSON instead of XML in FlexForm fields added
Actions #3

Updated by Georg Ringer over 3 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?

Actions #4

Updated by Bastian Stargazer over 3 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.

Actions #5

Updated by Bastian Stargazer almost 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.

Actions

Also available in: Atom PDF