Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: Nested Attributes #41642
    diegoliv
    Participant

    Just found out a quick workaround, using object spread to clone the main attribute:

    const changeArrowSize = ( value ) => {
        const styles = { ...props.attributes.navigationStyles };
        styles.arrowSize = value;
    
        props.setAttributes( { navigationStyles: styles } );
    };

    There might be a better way to do it, but for now this works, in case anyone has the same issue as me.

    Now, another question, kind of off topic since it’s not 100% javascript related, but more Gutenberg related. I want to store this object attribute as a post meta. For this, I understand that I need to set my attribute like this:

    navigationStyles: {
        type: 'object',
        default: {
            arrowSize: 10,
        },
        source: 'meta',
        meta: 'navigation_styles',
    }

    And then, I need to make this attribute visible as post meta for the Rest API. Something like this:

    function my_blocks_meta_init(){
        register_meta('post', 'navigation_styles', array(
            'show_in_rest' => true,
            'type' => '??????',
        ));
    }
    add_action('init', 'my_blocks_meta_init');

    Removing the type argument for the register_meta call will store this post meta as an array. That’s fine for PHP, js objects can sometimes be just like multidimensional arrays. But even with all of this kind of “working”, when I try to save a post with this post meta, I only get this message: Updating failed. I suspect that this is related to the type argument somehow. Single attributes, with simpler types, like a string, are correctly saved.

    Again, any tips to figure out this one? Thanks!

Viewing 1 post (of 1 total)