# How to convert a field type while preserving content?

**URL:** <https://forum.getkirby.com/t/how-to-convert-a-field-type-while-preserving-content/29588>\
**Category:** Questions\
**Tags:** v3\
**Created:** [September 27, 2023, 1:42pm UTC](https://forum.getkirby.com/t/how-to-convert-a-field-type-while-preserving-content/29588 "2023-09-27T13:42:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeriko](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@jeriko](https://forum.getkirby.com/u/jeriko)\
**Post date:** [September 27, 2023, 1:42pm UTC](https://forum.getkirby.com/t/how-to-convert-a-field-type-while-preserving-content/29588/1 "2023-09-27T13:42:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 27, 2023, 2:24pm UTC](https://forum.getkirby.com/t/how-to-convert-a-field-type-while-preserving-content/29588/2 "2023-09-27T14:24:18Z")

</div>

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:

> [@Creating structure field entries from frontend works but …](https://forum.getkirby.com/t/creating-structure-field-entries-from-frontend-works-but/28119):
>
> Hey! My goal is to build a counter for the amount of form submissions to an external form endpoint. So I came up the idea to write down each submit in a file and somebody linked to [Creating pages from frontend | Kirby CMS](https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend) but with the hint to do this with a strucured field instead. So I tinkered down the origin example and my testing code below is working. But if I add a value to the form’s action attribut, then the form itself works but there happens no entry in kirby anymore. How do I have t…

---

<div class="post-metadata">

**Author:** ![jeriko](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@jeriko](https://forum.getkirby.com/u/jeriko)\
**Post date:** [September 27, 2023, 4:24pm UTC](https://forum.getkirby.com/t/how-to-convert-a-field-type-while-preserving-content/29588/3 "2023-09-27T16:24:47Z")

</div>

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? 🙂

Thank you for pointing me in the right direction!
