Repository navigation
Description
Issue Overview
My goal is to have a block attribute stored in meta, that has a default value, for new posts that is not false. Currently the initial value of an attribute, stored as post meta, is always false.
Steps to Reproduce (for bugs)
- Create a block with an attribute saved as post meta.
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const el = wp.element.createElement;
registerBlockType( 'namespace/name', {
title: __( 'Name', 'text-domain' ),
icon: 'networking',
category: 'common',
attributes: {
showEmail : {
default: true,
type: 'boolean',
source: 'meta',
meta: 'prefix_showEmail'
}
},
edit({attributes, setAttributes, className, focus, id}) {
console.log(attributes.showEmail);
},
save({attributes, className}) {
return (
<div
className={className}
/>
);
},
} );- Use
register_metato register field in Rest API.
register_meta( 'post', 'prefix_showEmail', array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'schema' => array(
'default' => true
)
) );Notice that I tried to set the default value using schema.default because that's how the REST API expects it. See: https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php#L71
WordPress explicitly disallows, via the register_meta_args using an index of schema or default in that function. :(
3. Create a new post.
4. Add block to post.
5. Notice default value is false, not true.
6. Sad face emojis.
Expected Behavior
I expect the block attribute's default to be true, or whatever is set in the block.
Current Behavior
The default value is always false. The reason it is false, is that get_metadata() always returns false, before the filter on the return value runs if it's called.
Possible Solution
- In Gutenberg, if it's a new post, set block attributes that are meta from default.
- In core, allow register_meta to set a default. Currently
_wp_register_meta_args_whitelist()prevents doing so.
Todos
- Tests
- Documentation
Activity
jorgefilipecosta commentedon May 2, 2018
Hi @Shelob9,
Thank you for reporting this issue. I'm able to replicate it.
It looks like the root problem is a core issue.
For a post with meta registered as the provided in the sample:
register_meta( 'post', 'prefix_showEmail', array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'schema' => array(
'default' => true
)
) );
The rest API by default returns false for that meta field. I'm not sure if the schema.default should have been respected or not, but if not I think the rest API should signal that the field was not yet defined.
In the current implementation, Gutenberg has no way to differentiate between an undefined value and a value explicitly equal to false.
Shelob9 commentedon May 10, 2018
Related core ticket https://core.trac.wordpress.org/ticket/38323#comment:47
TimothyBJacobs commentedon May 11, 2018
@Shelob9 Its not documented properly, but the
show_in_restargument can also be an array.https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php#L350
designsimply commentedon Nov 1, 2018
@Shelob9 @danielbachhuber I noticed that https://core.trac.wordpress.org/ticket/38323#comment:47 is now closed as fixed. Is worked still needed on the Gutenberg side or is this issue okay to close?
apmatthews commentedon May 15, 2019
Just for reference, the previously mentioned core ticket didn't address this issue. There is another one in progress though. https://core.trac.wordpress.org/ticket/43941