Make WordPress Core

Opened 6 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#66229 closed enhancement (fixed)

Register and filter fields for DataViews/DataForm in the server

Reported by: oandregal Owned by: oandregal
Priority: normal Milestone: 7.2
Component: General Version: trunk
Severity: normal Keywords: has-patch has-unit-tests gutenberg-merge
Cc: Focuses:

Description

Related to https://core.trac.wordpress.org/ticket/65981

We want to introduce the ability to register and extend the fields used by DataViews/DataForm in the server for any given entity.

The whole initiative is tracked in Gutenberg at ​https://github.com/WordPress/gutenberg/issues/74865

Change History (20)

#1 @oandregal
6 weeks ago

  • Version → trunk

This ticket was mentioned in ​PR #13911 on ​WordPress/wordpress-develop by ​@oandregal.


6 weeks ago
#2

  • Keywords has-patch has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/66229
See: ​https://github.com/WordPress/gutenberg/pull/83368

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Opus 5.5
Used for: Implementation. Reviewed and edited by me.

#3 @waneezashafiq32
6 weeks ago

Tested PR #13911 on WordPress Playground (core-pr=13911).

Environment:

Testing performed:

  1. REST endpoint verification
    • GET /wp/v2/fields → 400 rest_missing_callback_param (kind, name required) ✅
    • GET /wp/v2/fields?kind=postType&name=page (unauthenticated) → 401 rest_cannot_read ✅
    • GET /wp/v2/fields?kind=postType&name=page (authenticated via wp.apiFetch) → 200 with empty fields ✅
  1. Field registration via fields_api_init hook Registered 2 custom fields on postType/page using WP_Fields_Registry::register():
    • my_test_field (text)
    • my_second_field (integer)

Confirmed signature: register( $origin, $kind, $name, $fields[], $script_module = null )

  1. REST response after registration GET /wp/v2/fields?kind=postType&name=page → { kind: 'postType', name: 'page', fields: Array(2), script_modules: Array(0) } Both fields returned correctly ✅
  1. Validation
    • Calling register() with incorrect arguments (missing 'id' in field definition) produces proper _doing_it_wrong() notice ✅
    • Duplicate fields are skipped as documented ✅

Result: PR works as described. The register() method correctly stores origin as
both 'origin' and 'registeredBy' properties on each field, and the REST
controller serves them per entity.

Note: During testing, I confirmed that the fields definition parameter must be
a list of arrays (each with an 'id' key), not a single field definition. The
docblock is clear about this, but it's worth noting for anyone writing their
first registration.

Props to @oandregal for the implementation.

#4 @wildworks
5 weeks ago

  • Keywords gutenberg-merge added

#5 @im3dabasia1
4 weeks ago

I’m going through the 7.2 tickets ahead of beta 1.

Thank you for the ​update, @oandregal. Are we targeting 7.2 for this? If it needs more time, we can move it to 7.3.

#6 @oandregal
4 weeks ago

@im3dabasia1 We're polishing some details over at Gutenberg and aim to get this merged for 7.2.

#7 @im3dabasia1
4 weeks ago

Thank you for the update, @oandregal. Great to hear!

​@oandregal commented on ​PR #13911:


3 weeks ago
#8

​https://github.com/WordPress/wordpress-develop/pull/14495 was merged and this rebased onto trunk.

This ticket was mentioned in ​PR #14512 on ​WordPress/wordpress-develop by ​@jorgefilipecosta.


3 weeks ago
#10

Backports the PHP of ​https://github.com/WordPress/gutenberg/pull/84696, a follow-up to the entity fields API that landed in r64528.

Gutenberg registers the core fields (title, author, status, the media fields…) on the server, through the entity fields API, and the editors read them from /wp/v2/entity-fields. Core has the API but registers no fields yet, so the title column of the site editor's pages list is blank. This PR makes Core register them.

This PR:

  • Copies the PHP files of @wordpress/core-fields from the Gutenberg build to wp-includes/build/scripts/core-fields/, in the gutenberg-php copy task that copies routes.php and pages.php, and loads its index.php from wp-settings.php the same way.
  • Makes the existing entity fields tests replace the core fields with their fixture defaults, so they keep testing the API alone.
  • Ports the tests of wp_register_core_field_collections() and wp_register_core_post_type_supports_fields().

Some notes:

Testing Instructions

  1. Set gutenberg.sha in package.json to trunk, then run npm run gutenberg:download and npm run build:dev.
  2. Open wp-admin/site-editor.php?p=%2Fpage and verify the pages list shows the titles, authors, and statuses of the pages.
  3. In the console, run await wp.apiFetch( { path: '/wp/v2/entity-fields?kind=postType&name=page' } ) and verify it returns the fields of pages with the @wordpress/core-fields/post_type_supports and @wordpress/core-fields/page modules.
  4. Open the templates, patterns, and media screens of the site editor and verify their fields, and the site identity screen with the site title, tagline, logo, and icon.
  5. Run npm run test:php -- --group entity-fields.

Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/84696

Use of AI Tools

AI assistance: Yes
Used for: porting the code and the tests from the Gutenberg PR, and drafting this description; reviewed and edited by me.

This ticket was mentioned in ​PR #14513 on ​WordPress/wordpress-develop by ​@jorgefilipecosta.


3 weeks ago
#11

Backports the view config changes of ​https://github.com/WordPress/gutenberg/pull/84700 into the core.

This PR adds the view configs that core was still missing:

  • attachment: the media editor form (title, alt text, caption and description, then date, author, filename, MIME type, file size, dimensions and "Attached to"). Without it, the media editor modal falls back to the default post type form and its "Details" tab shows only "Date added" and "Author".
  • wp_navigation: a list view sorted by date, newest first.

The root/site form, the wp_template reading_settings field and the group summaries of the default post type form are already in core.

Testing

Verify unit tests are passing:

vendor/bin/phpunit --group view-config

With ​https://github.com/WordPress/wordpress-develop/pull/14512 applied, open the "Edit image" modal from the Image block toolbar and check that the "Details" tab shows the attachment fields.

Drafted with AI assistance (Claude Code) and reviewed by me.

🤖 Generated with ​Claude Code

#13 @jorgefilipecosta
3 weeks ago

In 64567:

Editor: Add the attachment entity view config for the media editor.

Adds the view config of the attachment post type. Its form is the one of the media editor: the editable fields (title, alt text, caption, and description) in a regular layout, then the date, author, file name, MIME type, file size, dimensions, and "Attached to" fields in panels. Only the form is replaced; the default view, the layouts, and the list view stay the ones wp_get_entity_view_config() builds for every post type.
Without it, the media editor falls back to the default post type form, and the "Details" tab of the "Edit image" modal shows only the date and the author.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14513.
Synced from ​https://github.com/WordPress/gutenberg/pull/84700.

Props jorgefilipecosta, oandregal.
See #66229.

#15 @jorgefilipecosta
3 weeks ago

  • Resolution → fixed
  • Status assigned → closed

In 64581:

Editor: Register the core entity fields on the server.

Gutenberg registers the core fields (title, author, status, the media fields, …) on the server through the entity fields API, and the editors read them from the wp/v2/entity-fields route. Core has the API but registered no fields, so the title column of the Site Editor's pages list was blank. Register them in Core the same way.
Copy the PHP files of @wordpress/core-fields from the Gutenberg build in the gutenberg-php Grunt copy task, next to the core actions, and load their index.php from wp-settings.php. They register the default fields every post type gets from its supports, plus the field collections of pages, templates, template parts, patterns, attachments, navigation menus, and the site.
The entity fields tests now swap the core fields for their fixture defaults, so they still test the API on its own. The tests for the core fields are skipped until the pinned Gutenberg build includes them.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14512.
Synced from ​https://github.com/WordPress/gutenberg/pull/84696.
Follow-up to [64528].

Props oandregal, jorgefilipecosta.
Fixes #66229.

This ticket was mentioned in ​PR #14716 on ​WordPress/wordpress-develop by ​@oandregal.


3 weeks ago
#16

Follow-up to the Entity Actions and Entity Fields APIs.

Bug fixes

  • _wp_preload_entity_actions() and _wp_preload_entity_fields() assumed $paths was an array. If an earlier block_editor_rest_api_preload_paths callback returned null, false, '', a string or an object, the editor crashed. Empty values now become an array so the entity paths are still added. Other non-array values, or a context that isn't a WP_Block_Editor_Context, are returned unchanged. $context now defaults to null, so a call with one argument no longer throws an ArgumentCountError.
  • WP_Entity_Fields_Registry::get_all_registered_field_modules() returned numeric module ids as integers (from the array keys). They're now cast to strings, matching the actions registry, so the editor script declares the id as it was registered.

Code cleanup

The actions and fields code was duplicated. It now shares two helpers in a new entity-utils.php, loaded before both APIs:

  • _wp_add_entity_preload_paths( $paths, $context, $route ): used by both preload callbacks. The single-use helpers _wp_get_post_type_fields_preload_paths() and _wp_get_site_editor_screen_post_types() are inlined into it.
  • _wp_add_entity_modules_to_editor_script( $scripts, $get_modules ): used by both admin_footer callbacks. It takes a callable, not the modules, so the registry is still only read (and its init action only fired) on pages that load the editor script.

No behavior changes beyond the two fixes.

Testing

npm run test:php -- --group entity-actions,entity-fields

A new test covers numeric module ids. The unregistered post types test now goes through _wp_preload_entity_fields().

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Implementation, tests, and this summary; reviewed by me.

#17 @oandregal
3 weeks ago

In 64686:

Editor: Fix edge cases in the entity actions and fields APIs.

_wp_preload_entity_actions() and _wp_preload_entity_fields() assumed
$paths was an array, so the editor crashed when an earlier
block_editor_rest_api_preload_paths callback returned null, false,
a string or an object. Empty values are now turned into an array so the
entity paths are still added. Other non-array values, and contexts that
are not a WP_Block_Editor_Context, are returned unchanged. $context
now defaults to null, so calling either function with one argument no
longer throws an ArgumentCountError.

Numeric script module ids are now handled as strings. PHP turned them into
integer array keys, so wp_get_registered_entity_field_modules() and
wp_get_registered_entity_action_modules() could miss them in a strict
comparison and array_merge() could renumber them. Both functions now
return a list of entries holding the module id and the ids of the fields
or actions it applies to. This is the shape the REST controllers already
returned, and they now use these results directly. The entity fields
endpoint now returns module ids as strings, matching its schema.

A field registered with the id '0' is no longer rejected as empty.

The code duplicated between the two APIs now lives in a new
entity-utils.php file:

  • _wp_add_entity_preload_paths() builds the preload paths for both preload callbacks.
  • _wp_add_entity_modules_to_editor_script() adds the script modules to the editor script for both admin_footer callbacks. It takes a callable rather than the modules, so the registries are still only read on pages that load the editor script.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14716.

Props oandregal, jorgefilipecosta.
See #66229, #66343.

​@Joen commented on ​PR #14716:


3 weeks ago
#19

Noting that I'm seeing some errors that I think are caused by this PR, ​with context here.

​@oandregal commented on ​PR #14716:


3 weeks ago
#20

Thanks for the heads-up, I've just merged ​https://github.com/WordPress/gutenberg/pull/85390 that should fix them. I didn't expect Gutenberg's CI to pick the last WordPress version so fast! Should I know, I'd have synced the merging of both PRs. Sorry for the inconvenience.

Note: See TracTickets for help on using tickets.