Gutenberg: Default value of block attributes stored as meta not respected.

Created on 15 Jan 2018  路  5Comments  路  Source: WordPress/gutenberg

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)

  1. 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}
            />
        );
    },
} );
  1. Use 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. :(

  1. Create a new post.
  2. Add block to post.
  3. Notice default value is false, not true.
  4. 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

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.

Todos

  • [ ] Tests
  • [ ] Documentation
Core REST API Task REST API Interaction [Feature] Extensibility [Type] Bug [Type] WP Core Bug

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

All 5 comments

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 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,
        ]
    ]
] );

https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php#L350

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cr101 picture cr101  路  3Comments

pfefferle picture pfefferle  路  3Comments

franz-josef-kaiser picture franz-josef-kaiser  路  3Comments

davidsword picture davidsword  路  3Comments

jasmussen picture jasmussen  路  3Comments