Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

- Media modal: Show the fields registered for attachments on the server as columns and filters. ([#86457](https://github.com/WordPress/gutenberg/pull/86457))
- Notes: Add one note to several selected blocks, or to text selected across them ([#86051](https://github.com/WordPress/gutenberg/pull/86051)).
- Style revisions: Compare the selected revision with the current version side by side, as a visual preview or as a code diff of the styles and settings, with a header naming each ([#85288](https://github.com/WordPress/gutenberg/pull/85288)).
- RTC: Add an optional outline around the blocks a collaborator is working in. Turn it on in Preferences > General > Interface > "Outline collaborators' blocks" ([#86272](https://github.com/WordPress/gutenberg/pull/86272)).
Expand Down
19 changes: 19 additions & 0 deletions packages/editor/src/hooks/media-upload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const { MediaUploadModal: MediaUploadModalComponent } = unlock(
mediaUtilsPrivateApis
);

const EMPTY_FIELDS = [];

/**
* Supplies the modal with the post it was opened from, so that its "Attached to"
* filter can offer media uploaded to that post, labelled with the post type's
Expand All @@ -26,6 +28,11 @@ const { MediaUploadModal: MediaUploadModalComponent } = unlock(
* Their entity ID is a slug rather than a number besides, which the media query
* has no use for.
*
* It also supplies the fields registered for attachments on the server, so
* that a plugin's fields show in the modal as columns and filters. They are
* requested the first time the modal opens rather than wherever a media
* placeholder renders.
*
* @param {Object} props Props passed through to the modal.
*/
function MediaUploadModalWithPostContext( props ) {
Expand All @@ -44,11 +51,23 @@ function MediaUploadModalWithPostContext( props ) {
return { postId: currentPostId, postType: currentPostType };
}, [] );

const fields = useSelect(
( select ) =>
props.isOpen
? unlock( select( editorStore ) ).getEntityFields(
'postType',
'attachment'
)
: EMPTY_FIELDS,
[ props.isOpen ]
);

return (
<MediaUploadModalComponent
{ ...props }
postId={ postId }
postType={ postType }
fields={ fields }
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/media-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- `MediaUploadModal`: Query filters on attachment taxonomy fields by the taxonomy. ([#86458](https://github.com/WordPress/gutenberg/pull/86458))
- `MediaUploadModal`: Add a `fields` prop for fields registered for attachments, shown alongside the modal's own as columns and filters. ([#86457](https://github.com/WordPress/gutenberg/pull/86457))

## 6.1.0 (2026-11-04)

Expand Down
23 changes: 22 additions & 1 deletion packages/media-utils/src/components/media-upload-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ interface MediaUploadModalProps {
* template" — falling back to a generic label when it is absent.
*/
postType?: string;

/**
* Fields registered for attachments, such as a plugin's, shown alongside
* the modal's own and available as columns and filters. A field with the
* ID of one of the modal's own is left out: those carry behavior specific
* to the modal, such as the "Attached to" options for the current post.
*/
fields?: Field< RestAttachment >[];
}

/**
Expand All @@ -246,6 +254,7 @@ interface MediaUploadModalProps {
* @param props.searchLabel Label for search input
* @param props.postId ID of the post the modal was opened from
* @param props.postType Slug of that post's type
* @param props.fields Fields registered for attachments
* @return JSX element or null
*/
export function MediaUploadModal( {
Expand All @@ -263,6 +272,7 @@ export function MediaUploadModal( {
searchLabel = __( 'Search media' ),
postId,
postType,
fields: registeredFields,
}: MediaUploadModalProps ) {
// The "uploaded to this post" option resolves to this ID, and the media query
// has no use for anything else — a template's entity ID, for instance, is a
Expand Down Expand Up @@ -519,7 +529,7 @@ export function MediaUploadModal( {
totalPages,
} = useEntityRecordsWithPermissions( 'postType', 'attachment', queryArgs );

const fields: Field< RestAttachment >[] = useMemo(
const ownFields: Field< RestAttachment >[] = useMemo(
() => [
// Media field definitions from @wordpress/media-fields
// Cast is safe because RestAttachment has the same properties as Attachment
Expand Down Expand Up @@ -574,6 +584,17 @@ export function MediaUploadModal( {
[ attachedToPostId, uploadedToLabel ]
);

const fields = useMemo( () => {
if ( ! registeredFields?.length ) {
return ownFields;
}
const ownIds = new Set( ownFields.map( ( { id } ) => id ) );
return [
...ownFields,
...registeredFields.filter( ( { id } ) => ! ownIds.has( id ) ),
];
}, [ ownFields, registeredFields ] );

const actions: ActionButton< RestAttachment >[] = useMemo(
() => [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type Mock,
} from 'vitest';
import { createRegistry, RegistryProvider } from '@wordpress/data';
import type { Field } from '@wordpress/dataviews';
import { privateApis as coreDataPrivateApis } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { store as preferencesStore } from '@wordpress/preferences';
Expand Down Expand Up @@ -91,10 +92,17 @@ type ModalProps = {
value?: number | number[];
postId?: number;
postType?: string;
fields?: Field< any >[];
};

function renderModal(
{ isOpen = true, value, postId, postType }: Partial< ModalProps > = {},
{
isOpen = true,
value,
postId,
postType,
fields,
}: Partial< ModalProps > = {},
persistedView?: Record< string, unknown >
) {
const registry = createRegistry();
Expand Down Expand Up @@ -124,6 +132,7 @@ function renderModal(
value={ value }
postId={ postId }
postType={ postType }
fields={ fields }
onSelect={ onSelect }
onClose={ onClose }
/>
Expand Down Expand Up @@ -648,6 +657,77 @@ describe( 'MediaUploadModal', () => {
} );
} );

describe( 'registered fields', () => {
const genreField = {
id: 'genre',
type: 'array',
label: 'Genre',
elements: [
{ value: 3, label: 'Portrait' },
{ value: 4, label: 'Landscape' },
],
filterBy: { operators: [ 'isAny' ] },
} as Field< any >;

it( 'shows a registered field as a column', async () => {
mockRecords( IMAGE_RECORDS );
renderModal(
{ fields: [ genreField ] },
{ type: 'pickerTable', fields: [ 'genre' ] }
);

expect(
await screen.findByRole( 'columnheader', { name: /Genre/ } )
).toBeInTheDocument();
} );

it( 'offers a registered field as a filter', async () => {
const user = userEvent.setup();
renderModal( { fields: [ genreField ] } );

await user.click(
screen.getByRole( 'button', { name: 'Add filter' } )
);
await user.click(
await screen.findByRole( 'menuitem', { name: 'Genre' } )
);

expect(
await screen.findByRole( 'option', { name: 'Portrait' } )
).toBeVisible();
expect(
screen.getByRole( 'option', { name: 'Landscape' } )
).toBeVisible();
} );

it( "keeps the modal's own field when one is registered with its ID", async () => {
mockRecords( IMAGE_RECORDS );
renderModal(
{
fields: [
{
id: 'filename',
type: 'text',
label: 'Registered file name',
} as Field< any >,
],
},
{ type: 'pickerTable', fields: [ 'filename' ] }
);

expect(
await screen.findByRole( 'columnheader', {
name: /^File name/,
} )
).toBeInTheDocument();
expect(
screen.queryByRole( 'columnheader', {
name: /Registered file name/,
} )
).not.toBeInTheDocument();
} );
} );

it( 'updates the media query when the picker changes search', async () => {
const user = userEvent.setup();
const { registry } = renderModal();
Expand Down
Loading