Skip to content
81 changes: 26 additions & 55 deletions packages/block-editor/src/hooks/block-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,16 @@ import RichText from './rich-text';
import Media from './media';
import Link from './link';

const CONTROLS = {
richtext: RichText,
media: Media,
link: Link,
};

/**
* Creates a configured control component that wraps a custom control
* and passes configuration as props.
*
* @param {Component} ControlComponent The React component for the control.
* @param {string} type The type of control.
* @param {Object} config The control configuration passed as a prop.
*
* @return {Function} A wrapped control component
*/
function createConfiguredControl( ControlComponent, type, config ) {
if ( ! ControlComponent ) {
throw new Error( `Control type "${ type }" not found` );
}

function createConfiguredControl( ControlComponent, config = {} ) {
return function ConfiguredControl( props ) {
return <ControlComponent { ...props } config={ config } />;
};
Expand Down Expand Up @@ -104,51 +93,33 @@ function BlockFields( {

return blockTypeFields.map( ( fieldDef ) => {
const field = {
id: fieldDef.id,
label: fieldDef.label,
type: fieldDef.type, // Use the field's type; DataForm will use built-in or custom Edit
...fieldDef,
};

// If the field defines a `mapping`, then custom `getValue` and `setValue`
// implementations are provided.
// These functions map from the inconsistent attribute keys found on blocks
// to consistent keys that the field can use internally (and back again).
// When `mapping` isn't provided, we can use the field API's default
// implementation of these functions.
if ( fieldDef.mapping ) {
field.getValue = ( { item } ) => {
// Extract mapped properties from the block attributes
const mappedValue = {};
Object.entries( fieldDef.mapping ).forEach(
( [ key, attrKey ] ) => {
mappedValue[ key ] = item[ attrKey ];
}
);
return mappedValue;
};
field.setValue = ( { value } ) => {
const attributeUpdates = {};
Object.entries( fieldDef.mapping ).forEach(
( [ key, attrKey ] ) => {
attributeUpdates[ attrKey ] = value[ key ];
}
);
return attributeUpdates;
};
}

// Only add custom Edit component if one exists for this type
const ControlComponent = CONTROLS[ fieldDef.type ];
if ( ControlComponent ) {
// Use EditConfig pattern: Edit is an object with control type and config props
field.Edit = createConfiguredControl(
ControlComponent,
fieldDef.type,
{
clientId,
fieldDef,
}
);
// These should be custom Edit components, not replaced here.
//
// - rich-text control: it needs clientId
// - link control: does not need anything extra
// - media control: needs the Edit config
if (
'string' === typeof fieldDef.Edit &&
fieldDef.Edit === 'rich-text'
) {
field.Edit = createConfiguredControl( RichText, {
clientId,
} );
} else if (
'string' === typeof fieldDef.Edit &&
fieldDef.Edit === 'link'
) {
field.Edit = createConfiguredControl( Link );
} else if (
'object' === typeof fieldDef.Edit &&
fieldDef.Edit.control === 'media'
) {
field.Edit = createConfiguredControl( Media, {
...fieldDef.Edit,
} );
}

return field;
Expand Down
16 changes: 2 additions & 14 deletions packages/block-editor/src/hooks/block-fields/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ export function getUpdatedLinkAttributes( {
};
}

export default function Link( { data, field, onChange, config = {} } ) {
export default function Link( { data, field, onChange } ) {
const [ isLinkControlOpen, setIsLinkControlOpen ] = useState( false );
const { popoverProps } = useInspectorPopoverPlacement( {
isControl: true,
} );
const { fieldDef } = config;
const value = field.getValue( { item: data } );
const url = value?.url;
const rel = value?.rel || '';
Expand Down Expand Up @@ -148,21 +147,10 @@ export default function Link( { data, field, onChange, config = {} } ) {
);
} }
onRemove={ () => {
// Remove all link-related properties based on what's in the mapping
const removeValue = {};

if ( fieldDef?.mapping ) {
Object.keys( fieldDef.mapping ).forEach(
( key ) => {
removeValue[ key ] = undefined;
}
);
}

onChange(
field.setValue( {
item: data,
value: removeValue,
value: {},
} )
);
} }
Expand Down
30 changes: 9 additions & 21 deletions packages/block-editor/src/hooks/block-fields/media/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import { getMediaSelectKey } from '../../../store/private-keys';
import { store as blockEditorStore } from '../../../store';

function MediaThumbnail( { data, field, attachment, config } ) {
const { fieldDef } = config;
const { allowedTypes = [], multiple = false } = fieldDef.args || {};
const { allowedTypes = [], multiple = false } = config || {};

if ( multiple ) {
return 'todo multiple';
Expand Down Expand Up @@ -85,12 +84,11 @@ export default function Media( { data, field, onChange, config = {} } ) {
isControl: true,
} );
const value = field.getValue( { item: data } );
const { fieldDef } = config;
const { allowedTypes = [], multiple = false } = fieldDef.args || {};

// Check if featured image is supported by checking if it's in the mapping
const hasFeaturedImageSupport =

@oandregal oandregal Jan 13, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the kind of thing that's hidden behind a custom API: we were using the mapping instead of the Edit config to decide whether to enable featuredImage for the media control. See cover block.

fieldDef?.mapping && 'featuredImage' in fieldDef.mapping;
const {
allowedTypes = [],
multiple = false,
useFeaturedImage = false,
} = config;

const id = value?.id;
const url = value?.url;
Expand Down Expand Up @@ -140,23 +138,14 @@ export default function Media( { data, field, onChange, config = {} } ) {
multiple={ multiple }
popoverProps={ popoverProps }
onReset={ () => {
// Build reset value dynamically based on mapping
const resetValue = {};

if ( fieldDef?.mapping ) {
Object.keys( fieldDef.mapping ).forEach( ( key ) => {
resetValue[ key ] = undefined;
} );
}

onChange(
field.setValue( {
item: data,
value: resetValue,
value: {},
} )
);
} }
{ ...( hasFeaturedImageSupport && {
{ ...( useFeaturedImage && {
useFeaturedImage: !! value?.featuredImage,
onToggleFeaturedImage: () => {
onChange(
Expand All @@ -171,14 +160,13 @@ export default function Media( { data, field, onChange, config = {} } ) {
} ) }
onSelect={ ( selectedMedia ) => {
if ( selectedMedia.id && selectedMedia.url ) {
// Build new value dynamically based on what's in the mapping
const newValue = {
...selectedMedia,
mediaType: selectedMedia.media_type,
};

// Turn off featured image when manually selecting media
if ( hasFeaturedImageSupport ) {
if ( useFeaturedImage ) {
newValue.featuredImage = false;
}

Expand Down
18 changes: 12 additions & 6 deletions packages/block-library/src/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,25 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
id: 'audio',
label: __( 'Audio' ),
type: 'media',
mapping: {
id: 'id',
url: 'src',
},
args: {
Edit: {
control: 'media', // TODO: replace with custom component
allowedTypes: [ 'audio' ],
multiple: false,
},
getValue: ( { item } ) => ( {
id: item.id,
url: item.src,
} ),
setValue: ( { value } ) => ( {
id: value.id,
src: value.url,
} ),
Comment on lines +50 to +57

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the reasons I'd avoided moving these functions here is that a lot of this config would ideally be migrated to the attribute definition in block.json where it isn't possible to define a function.

The same goes for Edit.control where a string would have to be used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, we need this to be declarative, agree to that.

The value of this direction is that it highlights as early as possible what we need to improve in the Fields/DataForm API to ship content-only. It is telling us the "problems to solve". Whereas, circumventing the APIs by using mapping/args hides those issues, and gives us the illusion that they are mature enough and that we're ready to ship (when we're not).

},
{
id: 'caption',
label: __( 'Caption' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
];
settings[ formKey ] = {
Expand Down
21 changes: 14 additions & 7 deletions packages/block-library/src/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,24 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
{
id: 'text',
label: __( 'Content' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
{
id: 'link',
label: __( 'Link' ),
type: 'link',
mapping: {
url: 'url',
rel: 'rel',
linkTarget: 'linkTarget',
},
type: 'url',
Edit: 'link', // TODO: replace with custom component
getValue: ( { item } ) => ( {
url: item.url,
rel: item.rel,
linkTarget: item.linkTarget,
} ),
setValue: ( { value } ) => ( {
url: value.url,
rel: value.rel,
linkTarget: value.linkTarget,
} ),
},
];
settings[ formKey ] = {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
{
id: 'content',
label: __( 'Code' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
];
settings[ formKey ] = {
Expand Down
25 changes: 17 additions & 8 deletions packages/block-library/src/cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,28 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
id: 'background',
label: __( 'Background' ),
type: 'media',
mapping: {
mediaType: 'backgroundType',
id: 'id',
url: 'url',
alt: 'alt',
featuredImage: 'useFeaturedImage',
},
args: {
Edit: {
control: 'media', // TODO: replace with custom component
// TODO - How to support custom gradient?
// Build it into Media, or use a custom control?
allowedTypes: [ 'image', 'video' ],
multiple: false,
useFeaturedImage: true,
},
getValue: ( { item } ) => ( {
id: item.id,
url: item.url,
alt: item.alt,
mediaType: item.backgroundType,
featuredImage: item.useFeaturedImage,
} ),
setValue: ( { value } ) => ( {
id: value.id,
url: value.url,
alt: value.alt,
mediaType: value.backgroundType,
useFeaturedImage: value.featuredImage,
} ),
Comment on lines +77 to +90

@andrewserong andrewserong Jan 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mediaType doesn't appear to be working properly here. I think it needs to map to backgroundType? Currently if I set it to a video background it doesn't work as it's attempting to use the mp4 url for the background image instead of as a video background:

Image

Similarly, the block attribute for this block is useFeaturedImage and not featuredImage, so the toggle doesn't appear to work for that:

Image

Whereas it should be selected like this (trunk):

Image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for the testing. I've got some mapping reversed, pushed some fixes. I'm going to test now .

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this block, and it's working as trunk. Note that there's an issue by which the second time you select a media (e.g., from image to video or vice versa) the second media doesn't show up. This also happens in trunk so I'm going to leave it out of scope for this PR.

This is trunk:

Screen.Recording.2026-01-14.at.11.44.38.mov

This is this branch:

Screen.Recording.2026-01-14.at.11.49.53.mov

},
];
settings[ formKey ] = {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/details/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
{
id: 'summary',
label: __( 'Summary' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
];
settings[ formKey ] = {
Expand Down
21 changes: 14 additions & 7 deletions packages/block-library/src/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,31 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
id: 'file',
label: __( 'File' ),
type: 'media',
mapping: {
id: 'id',
url: 'href',
},
args: {
Edit: {
control: 'media', // TODO: replace with custom component
allowedTypes: [],
multiple: false,
},
getValue: ( { item } ) => ( {
id: item.id,
url: item.href,
} ),
setValue: ( { value } ) => ( {
id: value.id,
href: value.url,
} ),
},
{
id: 'fileName',
label: __( 'Filename' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
{
id: 'downloadButtonText',
label: __( 'Button Text' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
];
settings[ formKey ] = {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ if ( window.__experimentalContentOnlyInspectorFields ) {
{
id: 'content',
label: __( 'Content' ),
type: 'richtext',
type: 'text',
Edit: 'rich-text', // TODO: replace with custom component
},
];
settings[ formKey ] = {
Expand Down
Loading
Loading