Field value of a "new" field gets deleted when I change the template

Hi there,

I am working with a page.changeStatus:after hook. In this hook, I change my template from project to publishedproject if I set the status to listed (for permission fine tuning reasons) and vice versa from publishedproject back to project when I change it back to unlisted or draft.

I have a field palette that only exists in the template publishedproject. Now, when I change the status from draft to published, the new palette field will be created with no value, as expected.

Here’s the problem: When I now set a value to that field, and change the status back to draft (and therefore the template changes back to project), it keeps the field with the set value (which is good). If I now change the status back to listed (and therefore back to the publishedproject template, it seems to newly create the palette field and deletes what was saved in that field. It seems like Kirby is not checking if the field already exists in the content file, but just creates it on the template change and therefore deletes the previously saved value.

Is this the intended behaviour? Or should I report it as a bug? Either way, how can I work around this and keep the value, even if I switch the template?

Thanks!

I’d try to create a field with this name in the other template as well, as a hidden field.

That sounds like it might work, but giving this a try, it actually makes things worse.

In this case, it deletes the field altogether in the moment when I change the status from listed back to draft. So applying the hidden field seems to delete the value. :disappointed:

Then that’s probably because the field types are not compatible.

But what about the initial problem? Should Kirby not keep the original value when I change the template?

I think Kirby deletes those field on purpose. At least I remember there was a discussion a long time ago if fields that don’t exist in both templates should be kept or not.

No that’s the thing: The field is kept in the moment, when I change it to a template where the field does not exist, but the value of the field is deleted when I change it to the template that actually has the field.

I guess I might file a bug report about this specific scenario. Then devs can still decide if this is intentional or not, but I don’t see any reason why the value of the existing field should be deleted in that moment.

My guess is that the value is not remove when you change to draft because drafts aren’t validated but published pages are.

But yes, create and issue.

Ah, ok. So it might even be different, if the “draft template” had that field and the “published template” had not. Interesting.

Will do, thanks!

P.S.: Should I mark such “unresolved” questions as solved as there is no further need for discussion, or do I just leave them open?

We can leave it open until we have some result.

1 Like