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.
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}
/>
);
},
} );
register_meta to 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. :(
I expect the block attribute's default to be true, or whatever is set in the block.
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.
1) In Gutenberg, if it's a new post, set block attributes that are meta from default.
2) In core, allow register_meta to set a default. Currently _wp_register_meta_args_whitelist() prevents doing so.
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.
Related core ticket https://core.trac.wordpress.org/ticket/38323#comment:47
@Shelob9 Its not documented properly, but the show_in_rest argument can also be an array.
register_meta( 'post', '_price', [
'show_in_rest' => [
'schema' => [
'type' => 'number',
'minimum' => 1.00,
]
]
] );
@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?
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
Most helpful comment
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