diff --git a/packages/editor/CHANGELOG.md b/packages/editor/CHANGELOG.md index c88737a61db8e5..8268b179f83778 100644 --- a/packages/editor/CHANGELOG.md +++ b/packages/editor/CHANGELOG.md @@ -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)). diff --git a/packages/editor/src/hooks/media-upload.jsx b/packages/editor/src/hooks/media-upload.jsx index 98926d0d14ff82..79088b411bf94a 100644 --- a/packages/editor/src/hooks/media-upload.jsx +++ b/packages/editor/src/hooks/media-upload.jsx @@ -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 @@ -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 ) { @@ -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 ( ); } diff --git a/packages/media-utils/CHANGELOG.md b/packages/media-utils/CHANGELOG.md index 9acd39969b80c9..8f1d5114aafbc7 100644 --- a/packages/media-utils/CHANGELOG.md +++ b/packages/media-utils/CHANGELOG.md @@ -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) diff --git a/packages/media-utils/src/components/media-upload-modal/index.tsx b/packages/media-utils/src/components/media-upload-modal/index.tsx index 4b23f9912a3864..14cf8e86041a35 100644 --- a/packages/media-utils/src/components/media-upload-modal/index.tsx +++ b/packages/media-utils/src/components/media-upload-modal/index.tsx @@ -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 >[]; } /** @@ -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( { @@ -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 @@ -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 @@ -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( () => [ { diff --git a/packages/media-utils/src/components/media-upload-modal/test/index.jsdom.test.tsx b/packages/media-utils/src/components/media-upload-modal/test/index.jsdom.test.tsx index d42f629db1b9a3..228a6ac8cad9cf 100644 --- a/packages/media-utils/src/components/media-upload-modal/test/index.jsdom.test.tsx +++ b/packages/media-utils/src/components/media-upload-modal/test/index.jsdom.test.tsx @@ -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'; @@ -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(); @@ -124,6 +132,7 @@ function renderModal( value={ value } postId={ postId } postType={ postType } + fields={ fields } onSelect={ onSelect } onClose={ onClose } /> @@ -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();