Opened 3 months ago
Last modified 3 months ago
#64215 new defect (bug)
slimImageObject drops meta fields added to attachments
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Media | Keywords: | needs-patch |
| Focuses: | Cc: |
Description
I've opened an issue in the Gutenberg repo, but since the solution likely involves modifying core code, I'm creating a ticket here as well.
### Description
The object passed to the onSelect callback in the Gutenberg MediaUpload component calls the following function that removes all image attributes except those in the attrSet constant:
const slimImageObject = ( img ) => {
const attrSet = [
'sizes',
'mime',
'type',
'subtype',
'id',
'url',
'alt',
'link',
'caption',
];
return attrSet.reduce( ( result, key ) => {
if ( img?.hasOwnProperty( key ) ) {
result[ key ] = img[ key ];
}
return result;
}, {} );
};
That means that meta fields added to attachments via register_post_meta and the attachment_fields_to_edit and attachment_fields_to_save filters are dropped. Is there a way to avoid this?
### Step-by-step reproduction instructions
- Register a meta field to the attachment post type:
register_post_meta(
'attachment',
'_oembed_url',
array(
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'sanitize_callback' => 'sanitize_url',
'auth_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
- Add a
MediaUploadcomponent to a block:
<MediaUpload
allowedTypes={['image']}
multiple={true}
gallery={true}
value={images.map((image) => image.id)}
onSelect={setImages}
render={({ open }) => (
<ToolbarButton onClick={open}>
{__('Edit images')}
</ToolbarButton>)}
/>
- Check that the
valueparam doesn't contain any meta fields:
const setImages = (value) => {
console.log(value);
}
Note: See
TracTickets for help on using
tickets.
The link to the GB issue: https://github.com/WordPress/gutenberg/issues/64415