Extract entity view config into a filterable API#78977
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| $kind = $request->get_param( 'kind' ); | ||
| $name = $request->get_param( 'name' ); | ||
|
|
||
| // TODO: this data will come from a registry of view configs per entity. |
There was a problem hiding this comment.
This code has been moved (and restructured) verbatim into the view-config-api.php file.
There was a problem hiding this comment.
Pull request overview
This pull request extracts entity “view config” construction out of the WordPress 7.1 REST controller into a standalone API function, so entity-specific configs can be provided/overridden via dynamic filters while keeping the /wp/v2/view-config response shape unchanged.
Changes:
- Added
gutenberg_get_entity_view_config( $kind, $name )as the central builder for view config defaults and a dynamic per-entity filter hook. - Moved the previously hardcoded per-entity configurations (page/post/patterns/template parts/templates) into filter callbacks registered in the new API file.
- Simplified
Gutenberg_REST_View_Config_Controller_7_1::get_items()to delegate config building to the new API.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/load.php | Loads the new view-config API alongside existing WP 7.1 REST compat code. |
| lib/compat/wordpress-7.1/view-config-api.php | Introduces the filterable view-config API and registers per-entity default configs via filters. |
| lib/compat/wordpress-7.1/class-gutenberg-rest-view-config-controller-7-1.php | Removes embedded config-building logic and uses gutenberg_get_entity_view_config() instead. |
| return apply_filters( | ||
| "get_entity_view_config_{$kind}_{$name}", | ||
| $config, | ||
| array( | ||
| 'kind' => $kind, | ||
| 'name' => $name, | ||
| 'all_items_title' => $all_items_title, | ||
| ) | ||
| ); |
There was a problem hiding this comment.
This API is inspired by how the existing table list / admin screen works, that allows 3rd parties to filter it (e.g., WP_Posts_List_Table, screen's get_hidden_columns, or WP_List_Table.
A few alternate designs that were considered:
- Use of multiple filters (e.g., one per top-level config) vs. a single one (existing approach). While it sounds sensible to use multiple filters at first, consumers will run into issues quickly: some of these configs may depend on each other (e.g., see how
default_layoutsis used to build theview_list), and so having multiple filters will require us passing the whole config anyway. - Filter callback arguments. One could argue that a dynamic filter would not need to pass
$kindand$nameas arguments because the filter is bound to that entity. Take a couple of filters such asget_entity_view_config_postType_wp_templateandget_entity_view_config_postType_pageas examples. If a consumer binds the same filter callback to multiple filters, how would it extract the$kindand$namefrom those filters? It becomes a bit tricky given both$kindand$namecan have underscores. - Filter callback arity. While it's useful to provide
$kind,$name, and even$all_items_titleto filter callbacks that want to use them, the majority of them won't need them. Instead of incrementing the filter callback arity with 3 arguments, I thought we'd bundle everyone of these into a single$entityarray. This approach is also more future proof: if we want to add new items in the future, it won't be a breaking change.
|
Flaky tests detected in 8cbd047. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/27012802088
|
- Rename per-entity callbacks to snake_case (PHPCS WordPress.NamingConventions) - Remove blank line before class closing brace - Add PR to existing wordpress-develop #11272 backport changelog Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
PHPCS flags the unused parameter; callbacks that don't read entity context now accept only $config and register with one accepted arg. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
4a94255 to
8cbd047
Compare
Part of #76544
Follows-up on #76573 (comment)
What?
Extracts the per-entity view configuration logic out of
Gutenberg_REST_View_Config_Controller_7_1into a standalone, filterable API.No change to the REST response shape (
default_view,default_layouts,view_list,form).Why?
Previously, the view configuration for every entity was hardcoded inside the REST controller and was not filterable.
Moving the logic behind a dynamic filter:
kind/name(e.g. custom post types, custom entity kinds).How?
lib/compat/wordpress-7.1/view-config-api.phpwith a publicgutenberg_get_entity_view_config( $kind, $name )function.get_entity_view_config_{$kind}_{$name}filter so core and third parties can provide entity-specific configuration.postType×page,post,wp_block,wp_template_part,wp_template) are moved out of private controller methods and re-registered as filter callbacks./wp/v2/view-config) now keeps only the routing, permission, and schema plumbing and delegates data building togutenberg_get_entity_view_config().Testing Instructions
These screens are powered by the
/wp/v2/view-configendpoint viagetViewConfig/useViewConfig(@wordpress/views). Confirm the refactor produces identical behavior.npm run buildornpm run dev) and activate the plugin.default_view,default_layouts,view_list):wp_block) — grid layout and its view list.formconfig: featured image, excerpt, status/scheduled date/password/sticky, slug, author, template, discussion, parent, format).trunkfor each entity:wp.apiFetch( { path: '/wp/v2/view-config?kind=postType&name=page' } ).then( console.log )— repeat forpost,wp_block,wp_template,wp_template_part.trunk—default_view,default_layouts,view_list, andformshould be byte-for-byte equivalent.perPageis now5.kind/namewith no registered filter (e.g.kind=postType&name=attachment) still returns the generic default config without errors.edit_posts) returnsrest_cannot_read/ 401.Use of AI Tools
This pull request was prepared with the assistance of AI tooling (Claude Code). All changes were reviewed by the author, who takes responsibility for them.