Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value of block attributes stored as meta not respected. #4494

Closed
2 tasks
Shelob9 opened this issue Jan 15, 2018 · 5 comments
Closed
2 tasks

Default value of block attributes stored as meta not respected. #4494

Shelob9 opened this issue Jan 15, 2018 · 5 comments
Assignees
Labels
Core REST API Task Task for Core REST API efforts [Feature] Extensibility The ability to extend blocks or the editing experience REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked.

Comments

@Shelob9
Copy link
Contributor

Shelob9 commented Jan 15, 2018

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. :(
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

  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
@jeffpaul jeffpaul added the [Type] Bug An existing feature does not function as intended label Jan 26, 2018
@jorgefilipecosta
Copy link
Member

jorgefilipecosta commented 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.

@jorgefilipecosta jorgefilipecosta added [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked. Core REST API Task Task for Core REST API efforts labels May 2, 2018
@Shelob9
Copy link
Contributor Author

Shelob9 commented May 10, 2018

@TimothyBJacobs
Copy link
Member

@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

@danielbachhuber danielbachhuber added the [Feature] Extensibility The ability to extend blocks or the editing experience label Jun 19, 2018
@desrosj desrosj self-assigned this Aug 14, 2018
@mtias mtias removed this from the Merge: Plugins milestone Oct 3, 2018
@danielbachhuber danielbachhuber added REST API Interaction Related to REST API and removed REST API Interaction Related to REST API labels Oct 8, 2018
@danielbachhuber danielbachhuber added this to the WordPress 5.0 milestone Oct 29, 2018
@designsimply
Copy link
Member

@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?

@mtias mtias closed this as completed Nov 16, 2018
@apmatthews
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core REST API Task Task for Core REST API efforts [Feature] Extensibility The ability to extend blocks or the editing experience REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked.
Projects
None yet
Development

No branches or pull requests

9 participants