WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 6 months ago

#50226 new defect (bug)

Updating block attribute meta (type array) fails if it is the only meta attribute

Reported by: mikethicke1 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4.1
Component: Editor Keywords:
Focuses: Cc:

Description

My support thread is here.

If I register a meta like this:

<?php
register_post_meta(
        $this->object_post_type->options['type'],
        'wpm_gallery_attach_ids',
        [
                'type' => 'array',
                'description' => 'Associated Images',
                'single' => true,
                'show_in_rest' => [
                        'schema' => [
                                'type' => 'array',
                                'items' => [
                                        'type' => 'number',
                                ],
                        ],
                ],
                'auth_callback'    => function() {
                        return current_user_can( 'edit_posts' );
                },
        ]
);

And a block like this:

<?php
register_block_type(
        'wp-museum/object-image-attachments-block',
        [
                'attributes' => [
                        'imgAttach' => [
                                'type'   => 'array',
                                'source' => 'meta',
                                'meta'   => 'wpm_gallery_attach_ids',
                                'items'  => [
                                                type' => 'number',
                                ],
                        ],
                ],
        ]
);

Then the array shows up correctly in my block's attributes as an array. If I update it like so:

const moveItem = ( imgId, move ) => {
	const imgIndex = imgAttach.findIndex( id => id === imgId );
	const newIndex = imgIndex + move;
	if ( newIndex < 0 || newIndex >= imgAttach.length ) {
		return;
	}
	imgAttach[ imgIndex ] = imgAttach[ newIndex ];
	imgAttach[ newIndex ] = imgId;
		
	const updatedImgAttach = [ ...imgAttach ];
	setAttributes( { 
		imgAttach: updatedImgAttach,
	} );
}

When I save the post, the attribute reverts to its original state and is not updated in the database.

However, if I add a second string meta to the block, everything works!

<?php
register_post_meta(
        $this->object_post_type->options['type'],
        'wpm_gallery_attach_ids_string',
        [
                'type' => 'string',
                'description' => 'Associated Images String',
                'single' => true,
                'show_in_rest' => true,
                'auth_callback'    => function() {
                        return current_user_can( 'edit_posts' );
                },
        ]
);
<?php
register_block_type(
        'wp-museum/object-image-attachments-block',
        [
                'attributes' => [
                        'imgAttach' => [
                                'type'   => 'array',
                                'source' => 'meta',
                                'meta'   => 'wpm_gallery_attach_ids',
                                'items'  => [
                                        'type' => 'number',
                                ],
                        ],
                        'imgAttachStr' => [
                                'type'   => 'string',
                                'source' => 'meta',
                                'meta'   => 'wpm_gallery_attach_ids_string',
                        ],
                ],
        ]
);
setAttributes( { 
	imgAttach: updatedImgAttach,
	imgAttachStr: JSON.stringify( updatedImgAttach )
} );

(To be clear, imgAttach is never set from imgAttachStr.)

This seems like the inverse of SetAttributes does not work as expected with two or more post meta fields #17384 . It *does* work with two or more meta fields, but not one? If it is an array?

I am using WordPress 5.4.1, without the Gutenberg plugin.

Change History (1)

#1 @SergeyBiryukov
6 months ago

  • Component changed from General to Editor
Note: See TracTickets for help on using tickets.