Parse YAML value in custom field


#1

I’m writing a custom field in Kirby 3 and I save data in the field’s vue component like this

this.$emit('input', { 
    test: 2 
})

The value seems to be saved correctly when I look in the content of the page containing my custom field.

MyCustomField: 

test: 2

But when I try to get the saved value in my vue component

props: {
    value: Object
}

the value is a string in yaml format.
Is there an easy way to automatically parse the value as a javascript object?
thanks,
Arno


#2

You can create a value prop in your PHP file that will parse the field value before it gets sent to the API and thus to the Vue component:

<?php 

return [
    'props' => [
        'value' => function ($value = null) {
            return Yaml::decode($value);
        },
    ]
];