JavaScript for WordPress › Forums › Gutenberg Development › Meta data – default value
- This topic has 6 replies, 2 voices, and was last updated 5 years, 8 months ago by ldecoker.
-
AuthorPosts
-
January 31, 2019 at 3:48 pm #87315ldecokerParticipant
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!
January 31, 2019 at 3:58 pm #87324Zac GordonKeymasterCorrect. 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.
January 31, 2019 at 4:41 pm #87329ldecokerParticipantThx 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.
January 31, 2019 at 7:03 pm #87340Zac GordonKeymasterIf 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!
February 1, 2019 at 7:47 am #87468ldecokerParticipantSorry 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!
February 1, 2019 at 1:32 pm #87494Zac GordonKeymasterOkay, 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..
February 1, 2019 at 1:50 pm #87496ldecokerParticipantokidoki. I will keep you informed about their answer.
have a nice day!
-
AuthorPosts
- You must be logged in to reply to this topic.