How to convert a field type while preserving content?

Assume I have a text field in a blueprint that’s been used by several pages. Now me/the client/someone else needs additional data related to that text field, so I modify the blueprint, change that field to structure, and make sure that I include that original field in the structure while adding the additional fields I need.

Question is what would be the best way to preserve/convert the data from the text field I already have? Obviously the original text field data isn’t shown in the panel anymore, yet it’s still available in the .txt as long as I don’t overwrite it. I’m guessing a script, but maybe there’s a better way?

You can do that via $page->update(), fetching the value from the text field and creating an array, that you store yml encoded in the structure field.

Check out the addToStructure() function here to get a head start:

So instead of changing the current field type I add a new field, fetch the value from the old one, insert it in the new one along with the new data and only then delete the old one. Yeah, that makes sense, why didn’t I think of that? :slight_smile:

Thank you for pointing me in the right direction!