JavaScript for WordPress Forums Gutenberg Development Meta data – default value

This topic contains 6 replies, has 2 voices, and was last updated by  ldecoker 8 months, 2 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #87315

    ldecoker
    Participant

    Hello!

    When you add a meta field as attribute, it does not take into account the default value specify in the attributes definition.

    Example:

    const attributes = {
    	identifier: {
    		type: 'string',
    		default: 'home',
    		source: 'meta',
    		meta: 'blocks_identifier'
    	},
    };
    export default attributes;

    The default value is ignored and the meta field is displayed empty.
    I found in gutenberg github a solution for that:

    register_meta(
    			'post',
    			'blocks_identifier',
    			[
    				'type'         => 'string',
    				'single'       => true,
    				'show_in_rest' => [
    					'schema' => [
    						'type'    => 'string',
    						'default' => 'home',
    					]
    				],
    			]
    		);

    When adding the block on the page, the meta field has the default value BUT if you do not change it, it’s not saved in database.
    I checked the POST request when saving the post and I can see that the meta are included in it.

    Any idea?

    Thx!

    #87324

    Zac Gordon
    Keymaster

    Correct. I believe you do not want to have a default value for the meta field.

    One simple way here is if the value of the attribute is empty you can just write a conditional to use another value.

    #87329

    ldecoker
    Participant

    Thx Zac, but looking in the POST request, I can see that the attribute has a value : the default one. So I do not understand why it’s not saved in database.

    #87340

    Zac Gordon
    Keymaster

    If you load a default value then save the post does that save the default value to meta?

    Sorry not fully understanding what you’re asking!

    #87468

    ldecoker
    Participant

    Sorry to not be so clear 🙂
    When I load a default value and save the post, it does not save the default value to meta but the value is present in the POST request.

    POST request URL:
    /wp-json/wp/v2/pages/62?_locale=user

    POST request meta section:

    meta {
    blocks_identifier: "home"
    }

    When checking the database, in postmeta, the meta has not been added.
    Hope this is more clear.

    Thx!

    #87494

    Zac Gordon
    Keymaster

    Okay, so I believe what you’ve determined is it doesn’t work.

    If this is expected behavior from GB then you would need to create a work around to have this actually work. I’d suggest pinging the WP Core Editor Slack Channel to ask if this is a bug or expected.

    As mentioned initially I didn’t think default would work with meta fields, but it may be that this is a bug..

    #87496

    ldecoker
    Participant

    okidoki. I will keep you informed about their answer.

    have a nice day!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.