Skip to content

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

Closed

Description

@Shelob9

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

Activity

  1. added
    [Type] BugAn existing feature does not function as intended
    on Jan 26, 2018
  2. jorgefilipecosta commented on May 2, 2018

    @jorgefilipecosta
    Member

    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.

  3. Shelob9 commented on May 10, 2018

    @Shelob9
    ContributorAuthor
  4. TimothyBJacobs commented on May 11, 2018

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

  5. self-assigned this
    on Aug 14, 2018
  6. removed this from the Merge: Plugins milestone on Oct 3, 2018
  7. added this to the WordPress 5.0 milestone on Oct 29, 2018
  8. designsimply commented on Nov 1, 2018

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

  9. apmatthews commented on May 15, 2019

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

Metadata

Metadata

Assignees

Labels

Core REST API TaskTask for Core REST API effortsREST API InteractionRelated to REST API[Feature] ExtensibilityThe ability to extend blocks or the editing experience[Type] BugAn existing feature does not function as intended[Type] WP Core TicketRequires an upstream change from WordPress. Core Trac ticket should be linked.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions