[wp-trac] [WordPress Trac] #50226: Updating block attribute meta (type array) fails if it is the only meta attribute

WordPress Trac noreply at wordpress.org
Fri May 22 13:26:08 UTC 2020


#50226: Updating block attribute meta (type array) fails if it is the only meta
attribute
--------------------------+-----------------------------
 Reporter:  mikethicke1   |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  5.4.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 My support thread is [https://wordpress.org/support/topic/updating-block-
 attribute-meta-type-array/#post-12875806 here].

 If I register a meta like this:


 {{{#!php
 <?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
 <?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
 <?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
 <?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
 [https://github.com/WordPress/gutenberg/issues/17384 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50226>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list