Convert a retrieved record into JSON object as you have in clipboard

Hi Dato community,

Iā€™m trying to achieve something but not sure itā€™s possible ā€¦

Iā€™m retrieving a a list of records using the CMA API and I would like to extract the Modular content field inside and convert it as JSON compatible with the one you generate when ā€œCopy to clipboardā€ command save it into the localstorage.

What Iā€™m doing today is pretty simple:

const presets = await client.items.list({
                filter: {
                  // Filtering by the model with api_key "cat" and the model with ID of "dog"
                  type: "preset",
                },
                nested: true
              });

            console.log('Presets Modular field called blocks', presets.blocks);

All my presets have a Modular field called blocks (an array of blocks)

I would like to transform this array into a compatible object with what you have in localstorage.
Is it possible?

At the moment, I can retrive an array like this one :

[
    {
        "type": "item",
        "attributes": {
            "internal_name": "Fullwidth, default, left, no backdrop hero (no featured image)",
            "background_image": null,
            "featured_image": null,
            "body": {
                "type": "item",
                "attributes": {
                    "internal_name": "",
                    "suptitle": "Short surtitle example text",
                    "title": "Medium length placeholder heading",
                    "heading_level": {
                        "type": "item",
                        "attributes": {
                            "level": "1"
                        },
                        "relationships": {
                            "item_type": {
                                "data": {
                                    "id": "TZdH_kDlTYabcvY9MJ-8Ig",
                                    "type": "item_type"
                                }
                            }
                        },
                        "id": "c1Ik8cFHQYWacgxH9EQDOw"
                    },

......


whereas the expected object must be like:

[
  {
    "internal_name": "Fullwidth, default, left, no backdrop hero (no featured image)",
    "background_image": null,
    "featured_image": null,
    "body": {
      "internal_name": "",
      "suptitle": "Short surtitle example text",
      "title": "Medium length placeholder heading",
      "heading_level": {
        "level": "1",
        "itemId": null,
        "itemTypeId": "TZdH_kDlTYabcvY9MJ-8Ig"
      },
      "subtitle": "Short subtitle example text",
      "lead": [
        {
          "type": "paragraph",
          "children": [
            {
              "text": ""
            }
          ]
        }
      ],
      "text": [

....

That would be awesome to find a way to convert the first object into the second one.

Thanks for your help.

Best regards.

Is this in a plugin? Does ctx.itemToFormValues() work?

HI @roger ,

Thank you for your quick answer.

Iā€™m in a plugin and I retrieve my records using the CMA.
Iā€™m not manipulating the ctx.item object but an object I retrieve from a query with CMA API.
Maybe Iā€™m doing something wrong but, here is the object I get from my query:

{
    "id": "UjdCtTJCSJ6i-rwxOZBmUQ",
    "type": "item",
    "name": "My preset for Matteo",
    "description": "This is a cool preset that I can reuse",
    "category": "Sections",
    "tags": "page, service",
    "enabled": true,
    "preview": {
        "alt": null,
        "title": null,
        "custom_data": {},
        "focal_point": null,
        "upload_id": "GdjfFU7kQFuEcuN86hd3hQ"
    },
    "preset": "[\n  {\n    \"internal_name\": \"My super duper grid\",\n    \"layout\": \"12\",\n    \"area1\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"L44LNpWGTKOuIS3-hVwrAQ\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area2\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"UtrGURqSQLCfI5Y23eCDsg\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area3\": [],\n    \"area4\": [],\n    \"theme\": \"{\\n  \\\"height\\\": \\\"stretched\\\"\\n}\",\n    \"advanced\": \"{}\",\n    \"itemId\": null,\n    \"itemTypeId\": \"ZKpmVqlgQPyJZXhoFZse1Q\"\n  },\n  {\n    \"size\": \"small\",\n    \"itemId\": null,\n    \"itemTypeId\": \"Ju8oj1XrQ-C60fD0rgSPCQ\"\n  },\n  {\n    \"internal_name\": \"My super duper grid 2\",\n    \"layout\": \"12\",\n    \"area1\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"GdjfFU7kQFuEcuN86hd3hQ\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area2\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"UtrGURqSQLCfI5Y23eCDsg\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area3\": [],\n    \"area4\": [],\n    \"theme\": \"{\\n  \\\"height\\\": \\\"stretched\\\"\\n}\",\n    \"advanced\": \"{}\",\n    \"itemId\": null,\n    \"itemTypeId\": \"ZKpmVqlgQPyJZXhoFZse1Q\"\n  }\n]",
    "blocks": [
        {
            "type": "item",
            "attributes": {
                "internal_name": "Fullwidth, default, left, no backdrop hero (no featured image)",
                "background_image": null,
                "featured_image": null,
                "body": {
                    "type": "item",
                    "attributes": {
                        "internal_name": "",
                        "suptitle": "Short surtitle example text",
                        "title": "Medium length placeholder heading",
                        "heading_level": {
                            "type": "item",
                            "attributes": {
                                "level": "1"
                            },
                            "relationships": {
                                "item_type": {
                                    "data": {
                                        "id": "TZdH_kDlTYabcvY9MJ-8Ig",
                                        "type": "item_type"
                                    }
                                }
                            },
                            "id": "c1Ik8cFHQYWacgxH9EQDOw"
                        },
                        "subtitle": "Short subtitle example text",
                        "lead": {
                            "schema": "dast",
                            "document": {
                                "children": [
                                    {
                                        "children": [
                                            {
                                                "type": "span",
                                                "value": ""
                                            }
                                        ],
                                        "type": "paragraph"
                                    }
                                ],
                                "type": "root"
                            }
                        },
                        "text": {
                            "schema": "dast",
                            "document": {
                                "children": [
                                    {
                                        "children": [
                                            {
                                                "type": "span",
                                                "value": ""
                                            }
                                        ],
                                        "type": "paragraph"
                                    }
                                ],
                                "type": "root"
                            }
                        },
                        "buttons": [
                            {
                                "type": "item",
                                "attributes": {
                                    "internal_name": "",
                                    "lnf": "solid",
                                    "color": "main",
                                    "link": {
                                        "type": "item",
                                        "attributes": {
                                            "text": "Solid button",
                                            "link": "{}"
                                        },
                                        "relationships": {
                                            "item_type": {
                                                "data": {
                                                    "id": "IFr7YVxGR9Kk3Y3ikq4S5Q",
                                                    "type": "item_type"
                                                }
                                            }
                                        },
                                        "id": "WxEX_xENQRSGUlDvli9leg"
                                    }
                                },
                                "relationships": {
                                    "item_type": {
                                        "data": {
                                            "id": "XePqFxSTQWiA09qgvr3XMQ",
                                            "type": "item_type"
                                        }
                                    }
                                },
                                "id": "WidNchmnSq6mbpCNvsjWRA"
                            }
                        ],
                        "alignment": "left",
                        "advanced": "{}"
                    },
                    "relationships": {
                        "item_type": {
                            "data": {
                                "id": "ZyT7DXr4SFa3Z9Bl5Kqkmw",
                                "type": "item_type"
                            }
                        }
                    },
                    "id": "WK0EK2pmTm6JCCcFo6plnA"
                },
                "theme": "{\n  \"cs\": \"default\",\n  \"align\": \"left\",\n  \"backdrop\": \"default\",\n  \"container\": \"fullwidth\"\n}",
                "advanced": "{\n  \"class\": \"\",\n  \"context\": \"Hero\"\n}"
            },
            "relationships": {
                "item_type": {
                    "data": {
                        "id": "TIdkBsjCTFCyBbzIn9eecg",
                        "type": "item_type"
                    }
                }
            },
            "id": "AjTV_QcRRg2q3SIHATQI6w"
        },
        {
            "type": "item",
            "attributes": {
                "internal_name": "Fullwidth, inverted, center, small backdrop hero (no featured image)",
                "background_image": null,
                "featured_image": null,
                "body": {
                    "type": "item",
                    "attributes": {
                        "internal_name": "",
                        "suptitle": "Short surtitle example text",
                        "title": "Medium length placeholder heading",
                        "heading_level": {
                            "type": "item",
                            "attributes": {
                                "level": "1"
                            },
                            "relationships": {
                                "item_type": {
                                    "data": {
                                        "id": "TZdH_kDlTYabcvY9MJ-8Ig",
                                        "type": "item_type"
                                    }
                                }
                            },
                            "id": "EbnHLy8gSbW-7EDor7IbwA"
                        },
                        "subtitle": "Short subtitle example text",
                        "lead": {
                            "schema": "dast",
                            "document": {
                                "children": [
                                    {
                                        "children": [
                                            {
                                                "type": "span",
                                                "value": ""
                                            }
                                        ],
                                        "type": "paragraph"
                                    }
                                ],
                                "type": "root"
                            }
                        },
                        "text": {
                            "schema": "dast",
                            "document": {
                                "children": [
                                    {
                                        "children": [
                                            {
                                                "type": "span",
                                                "value": ""
                                            }
                                        ],
                                        "type": "paragraph"
                                    }
                                ],
                                "type": "root"
                            }
                        },
                        "buttons": [],
                        "alignment": "left",
                        "advanced": "{}"
                    },
                    "relationships": {
                        "item_type": {
                            "data": {
                                "id": "ZyT7DXr4SFa3Z9Bl5Kqkmw",
                                "type": "item_type"
                            }
                        }
                    },
                    "id": "Ue734e58SDin9u6HuH_P4A"
                },
                "theme": "{\n  \"cs\": \"default\",\n  \"align\": \"center\",\n  \"backdrop\": \"default\",\n  \"size\": \"small\",\n  \"container\": \"fullwidth\"\n}",
                "advanced": "{\n  \"class\": \"\",\n  \"context\": \"Hero\"\n}"
            },
            "relationships": {
                "item_type": {
                    "data": {
                        "id": "TIdkBsjCTFCyBbzIn9eecg",
                        "type": "item_type"
                    }
                }
            },
            "id": "edtt_xqRSMeJVOKBGiPGaA"
        }
    ],
    "item_type": {
        "id": "TkmnDqFjRZKVmuAmDIh5kg",
        "type": "item_type"
    },
    "creator": {
        "id": "61117",
        "type": "user"
    },
    "meta": {
        "created_at": "2024-11-06T10:24:42.927+00:00",
        "updated_at": "2024-11-08T18:34:19.526+00:00",
        "published_at": "2024-11-08T18:34:19.578+00:00",
        "publication_scheduled_at": null,
        "unpublishing_scheduled_at": null,
        "first_published_at": "2024-11-06T10:24:42.973+00:00",
        "is_valid": true,
        "is_current_version_valid": true,
        "is_published_version_valid": true,
        "status": "published",
        "current_version": "cEzfR_ZLRHuwRqci1oBkVg",
        "stage": null
    }
}

Iā€™ve just tried to call the method, directly on the object:

const item = await ctx.itemToFormValues(preset);

but Iā€™m getting the following error:

presetsField.tsx:99 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'item_type')

:frowning:

Thanks again for your help.

Best

Hey @ghislain.flandin,

Iā€™m getting a similar error in your object, but Iā€™m not sure whatā€™s causing it. May I please fork your env to a sandbox and do more testing there? Something in your record, I think, is causing issues with the ctx.itemToFormValues() method, but I need to play around more to figure out what it is.

Generally speaking, the ctx.itemToFormValues() method SHOULD work even with something fetched from the CMA. For example, on a test project, this works for me:

Source code for example plugin (click to open)
import {Canvas} from 'datocms-react-ui';
import {RenderItemFormSidebarPanelCtx} from "datocms-plugin-sdk";
import {useEffect, useState} from "react";
import {buildClient} from "@datocms/cma-client-browser"

// The block shape used by the "Copy to clipboard" functionality that gets saved to localStorage
export type DeserializedBlock = { itemId: string; itemTypeId: string };

// Our plugin doesn't necessarily have to be a sidebar; it just has to have ctx.itemToFormValues()
export function Sidebar({ctx}: { ctx: RenderItemFormSidebarPanelCtx }) {

    const recordIdToFetch = "Qjv5J71QS3qYP960vNfQHg"; // From a sample project

    const [blocksFromOtherRecord, setBlocksFromOtherRecord] = useState<DeserializedBlock[] | null>(null);

    useEffect(() => {
        (async () => {

            try {
                // Make sure to only use this plugin with trusted editors, because your API key is exposed to them on the client
                const client = buildClient({apiToken: import.meta.env.VITE_DATOCMS_API_KEY})

                // Different from items.find(); this returns a slightly shape that's easier to use with itemToFormValues()
                const response = await client.items.rawFind(recordIdToFetch, {nested: true})

                // Get the actual record from the response
                const {data} = response
                console.log('Original response', response)

                // Now we run that external record through the itemToFormValues() method from the plugin's ctx
                const convertedItem = await ctx.itemToFormValues(data)

                // Extract the blocks that we want
                const blocks = convertedItem['blocks'] as DeserializedBlock[];
                console.log('Converted blocks', blocks)

                setBlocksFromOtherRecord(blocks)
            } catch (error) {
                console.error('Conversion error', error);
            }
        })()
    }, [])

    return (
        <Canvas ctx={ctx}>
            <h2>Your record</h2>
            <code>{JSON.stringify(blocksFromOtherRecord)}</code>
        </Canvas>
    );
}

Given this original response from the CMAā€™s items.rawFind():

response

{
    "data": {
        "id": "Qjv5J71QS3qYP960vNfQHg",
        "type": "item",
        "attributes": {
            "title": "Test",
            "blocks": [
                {
                    "type": "item",
                    "attributes": {
                        "text": "This is a test",
                        "paragraph": "This is a paragraph"
                    },
                    "relationships": {
                        "item_type": {
                            "data": {
                                "id": "MYKbJS_kTX6xi8NCDr2woQ",
                                "type": "item_type"
                            }
                        }
                    },
                    "id": "BnX7APJ4QJGm7HCi3kQGyg"
                }
            ]
        },
        "relationships": {
            "item_type": {
                "data": {
                    "id": "FabFUsGtSyKJlrEnfutZYg",
                    "type": "item_type"
                }
            },
            "creator": {
                "data": {
                    "id": "104280",
                    "type": "account"
                }
            }
        },
        "meta": {
            "created_at": "2024-11-09T15:37:44.981+00:00",
            "updated_at": "2024-11-09T15:37:44.988+00:00",
            "published_at": "2024-11-09T15:37:45.024+00:00",
            "publication_scheduled_at": null,
            "unpublishing_scheduled_at": null,
            "first_published_at": "2024-11-09T15:37:45.024+00:00",
            "is_valid": true,
            "is_current_version_valid": true,
            "is_published_version_valid": true,
            "status": "published",
            "current_version": "JzJCu0OFS0W_V3mWgdfq9g",
            "stage": null
        }
    }
}

We get back these blocks, which should match the format used by the localStorage copy:

blocks

[
    {
        "text": "This is a test",
        "paragraph": "This is a paragraph",
        "itemId": "BnX7APJ4QJGm7HCi3kQGyg",
        "itemTypeId": "MYKbJS_kTX6xi8NCDr2woQ"
    }
]

However, this same thing isnā€™t quite working for me on the object you provided. Could you please try it yourself with the rawFind() in your own project, or else let me fork that env and do more testing?

Hi @roger ā€¦
Of course you can fork!

I did a quick test regarding your example and getting an error

const response = await client.items.rawFind("UjdCtTJCSJ6i-rwxOZBmUQ", {nested: true})
            // Get the actual record from the response
            const {data} = response
            console.log('Original response', response)

            // Now we run that external record through the itemToFormValues() method from the plugin's ctx
            const convertedItem = await ctx.itemToFormValues(data)
            console.log('Converted', convertedItem)

The console.log for original response is like this:

{
    "data": {
        "id": "UjdCtTJCSJ6i-rwxOZBmUQ",
        "type": "item",
        "attributes": {
            "name": "My preset for Matteo",
            "description": "This is a cool preset that I can reuse",
            "category": "Sections",
            "tags": "page, service",
            "enabled": true,
            "preview": {
                "alt": null,
                "title": null,
                "custom_data": {},
                "focal_point": null,
                "upload_id": "GdjfFU7kQFuEcuN86hd3hQ"
            },
            "preset": "[\n  {\n    \"internal_name\": \"My super duper grid\",\n    \"layout\": \"12\",\n    \"area1\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"L44LNpWGTKOuIS3-hVwrAQ\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area2\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"UtrGURqSQLCfI5Y23eCDsg\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area3\": [],\n    \"area4\": [],\n    \"theme\": \"{\\n  \\\"height\\\": \\\"stretched\\\"\\n}\",\n    \"advanced\": \"{}\",\n    \"itemId\": null,\n    \"itemTypeId\": \"ZKpmVqlgQPyJZXhoFZse1Q\"\n  },\n  {\n    \"size\": \"small\",\n    \"itemId\": null,\n    \"itemTypeId\": \"Ju8oj1XrQ-C60fD0rgSPCQ\"\n  },\n  {\n    \"internal_name\": \"My super duper grid 2\",\n    \"layout\": \"12\",\n    \"area1\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"GdjfFU7kQFuEcuN86hd3hQ\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area2\": [\n      {\n        \"internal_name\": \"\",\n        \"image\": {\n          \"alt\": null,\n          \"title\": null,\n          \"custom_data\": {},\n          \"focal_point\": null,\n          \"upload_id\": \"UtrGURqSQLCfI5Y23eCDsg\"\n        },\n        \"video\": null,\n        \"external_video\": null,\n        \"legend\": \"\",\n        \"advanced\": \"{}\",\n        \"itemId\": null,\n        \"itemTypeId\": \"dNh4P7RhS_WkA4t9YAmBhg\"\n      }\n    ],\n    \"area3\": [],\n    \"area4\": [],\n    \"theme\": \"{\\n  \\\"height\\\": \\\"stretched\\\"\\n}\",\n    \"advanced\": \"{}\",\n    \"itemId\": null,\n    \"itemTypeId\": \"ZKpmVqlgQPyJZXhoFZse1Q\"\n  }\n]",
            "blocks": [
                {
                    "type": "item",
                    "attributes": {
                        "internal_name": "Fullwidth, default, left, no backdrop hero (no featured image)",
                        "background_image": null,
                        "featured_image": null,
                        "body": {
                            "type": "item",
                            "attributes": {
                                "internal_name": "",
                                "suptitle": "Short surtitle example text",
                                "title": "Medium length placeholder heading",
                                "heading_level": {
                                    "type": "item",
                                    "attributes": {
                                        "level": "1"
                                    },
                                    "relationships": {
                                        "item_type": {
                                            "data": {
                                                "id": "TZdH_kDlTYabcvY9MJ-8Ig",
                                                "type": "item_type"
                                            }
                                        }
                                    },
                                    "id": "c1Ik8cFHQYWacgxH9EQDOw"
                                },
                                "subtitle": "Short subtitle example text",
                                "lead": {
                                    "schema": "dast",
                                    "document": {
                                        "children": [
                                            {
                                                "children": [
                                                    {
                                                        "type": "span",
                                                        "value": ""
                                                    }
                                                ],
                                                "type": "paragraph"
                                            }
                                        ],
                                        "type": "root"
                                    }
                                },
                                "text": {
                                    "schema": "dast",
                                    "document": {
                                        "children": [
                                            {
                                                "children": [
                                                    {
                                                        "type": "span",
                                                        "value": ""
                                                    }
                                                ],
                                                "type": "paragraph"
                                            }
                                        ],
                                        "type": "root"
                                    }
                                },
                                "buttons": [
                                    {
                                        "type": "item",
                                        "attributes": {
                                            "internal_name": "",
                                            "lnf": "solid",
                                            "color": "main",
                                            "link": {
                                                "type": "item",
                                                "attributes": {
                                                    "text": "Solid button",
                                                    "link": "{}"
                                                },
                                                "relationships": {
                                                    "item_type": {
                                                        "data": {
                                                            "id": "IFr7YVxGR9Kk3Y3ikq4S5Q",
                                                            "type": "item_type"
                                                        }
                                                    }
                                                },
                                                "id": "WxEX_xENQRSGUlDvli9leg"
                                            }
                                        },
                                        "relationships": {
                                            "item_type": {
                                                "data": {
                                                    "id": "XePqFxSTQWiA09qgvr3XMQ",
                                                    "type": "item_type"
                                                }
                                            }
                                        },
                                        "id": "WidNchmnSq6mbpCNvsjWRA"
                                    }
                                ],
                                "alignment": "left",
                                "advanced": "{}"
                            },
                            "relationships": {
                                "item_type": {
                                    "data": {
                                        "id": "ZyT7DXr4SFa3Z9Bl5Kqkmw",
                                        "type": "item_type"
                                    }
                                }
                            },
                            "id": "WK0EK2pmTm6JCCcFo6plnA"
                        },
                        "theme": "{\n  \"cs\": \"default\",\n  \"align\": \"left\",\n  \"backdrop\": \"default\",\n  \"container\": \"fullwidth\"\n}",
                        "advanced": "{\n  \"class\": \"\",\n  \"context\": \"Hero\"\n}"
                    },
                    "relationships": {
                        "item_type": {
                            "data": {
                                "id": "TIdkBsjCTFCyBbzIn9eecg",
                                "type": "item_type"
                            }
                        }
                    },
                    "id": "AjTV_QcRRg2q3SIHATQI6w"
                },
                {
                    "type": "item",
                    "attributes": {
                        "internal_name": "Fullwidth, inverted, center, small backdrop hero (no featured image)",
                        "background_image": null,
                        "featured_image": null,
                        "body": {
                            "type": "item",
                            "attributes": {
                                "internal_name": "",
                                "suptitle": "Short surtitle example text",
                                "title": "Medium length placeholder heading",
                                "heading_level": {
                                    "type": "item",
                                    "attributes": {
                                        "level": "1"
                                    },
                                    "relationships": {
                                        "item_type": {
                                            "data": {
                                                "id": "TZdH_kDlTYabcvY9MJ-8Ig",
                                                "type": "item_type"
                                            }
                                        }
                                    },
                                    "id": "EbnHLy8gSbW-7EDor7IbwA"
                                },
                                "subtitle": "Short subtitle example text",
                                "lead": {
                                    "schema": "dast",
                                    "document": {
                                        "children": [
                                            {
                                                "children": [
                                                    {
                                                        "type": "span",
                                                        "value": ""
                                                    }
                                                ],
                                                "type": "paragraph"
                                            }
                                        ],
                                        "type": "root"
                                    }
                                },
                                "text": {
                                    "schema": "dast",
                                    "document": {
                                        "children": [
                                            {
                                                "children": [
                                                    {
                                                        "type": "span",
                                                        "value": ""
                                                    }
                                                ],
                                                "type": "paragraph"
                                            }
                                        ],
                                        "type": "root"
                                    }
                                },
                                "buttons": [],
                                "alignment": "left",
                                "advanced": "{}"
                            },
                            "relationships": {
                                "item_type": {
                                    "data": {
                                        "id": "ZyT7DXr4SFa3Z9Bl5Kqkmw",
                                        "type": "item_type"
                                    }
                                }
                            },
                            "id": "Ue734e58SDin9u6HuH_P4A"
                        },
                        "theme": "{\n  \"cs\": \"default\",\n  \"align\": \"center\",\n  \"backdrop\": \"default\",\n  \"size\": \"small\",\n  \"container\": \"fullwidth\"\n}",
                        "advanced": "{\n  \"class\": \"\",\n  \"context\": \"Hero\"\n}"
                    },
                    "relationships": {
                        "item_type": {
                            "data": {
                                "id": "TIdkBsjCTFCyBbzIn9eecg",
                                "type": "item_type"
                            }
                        }
                    },
                    "id": "edtt_xqRSMeJVOKBGiPGaA"
                }
            ]
        },
        "relationships": {
            "item_type": {
                "data": {
                    "id": "TkmnDqFjRZKVmuAmDIh5kg",
                    "type": "item_type"
                }
            },
            "creator": {
                "data": {
                    "id": "61117",
                    "type": "user"
                }
            }
        },
        "meta": {
            "created_at": "2024-11-06T10:24:42.927+00:00",
            "updated_at": "2024-11-08T18:34:19.526+00:00",
            "published_at": "2024-11-08T18:34:19.578+00:00",
            "publication_scheduled_at": null,
            "unpublishing_scheduled_at": null,
            "first_published_at": "2024-11-06T10:24:42.973+00:00",
            "is_valid": true,
            "is_current_version_valid": true,
            "is_published_version_valid": true,
            "status": "published",
            "current_version": "cEzfR_ZLRHuwRqci1oBkVg",
            "stage": null
        }
    }
}

But the ctx.itemToFormValues fails:

presetsField.tsx:128 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'reduce')

Thanks for your help.

PS: Iā€™m doing this test when excuting renderFieldExtension() method.

Quick note that Iā€™ve forked your main environment into a sandbox called datocms-support-main-copy-2024-11-1 and will be troubleshooting there further. Please donā€™t edit or delete that environment for the time being :slight_smile: Thanks!

Iā€™ll write back as soon as I have an update.

Interim update: Hmm, unfortunately, it looks like ctx.itemToFormValues() isnā€™t as pure as I thought :frowning: It loads some field data from the current model, which means that it only works while youā€™re inside the same item type (presets). If you try to load another modelā€™s fields via the CMA and pass it to the that method in another model type, it will crash.

Iā€™ll have to check with the devs on this. There might be a workaround by using ctx.editItem(yourPresetId) to load that other record, run ctx.itemToFormValues() there instead, and then either copy it to the clipboard or else save it into the plugin params (which can be reused across models)ā€¦ but that is super clunky and fragile, not to mention results in a terrible UX.

Ideally there would just be a way for us to expose a pure function where you can give it a CMA input and itā€™ll return a formValues output without needing to do its own lookups. Iā€™m not sure if thatā€™s possible right now; let me check with the devs and get back to you ASAP.

Weā€™ve improved ctx.itemToFormValues() so that it can now correctly parse lookups from any model, not just its own. This means that inside a plugin, you can use the CMA to fetch another record and then run it through itemToFormValues() anywhere itā€™s available and it should work.

The OP also had some other follow-up questions they sent over email, and weā€™ll answer those there.

1 Like