diff --git a/backport-changelog/7.2/13308.md b/backport-changelog/7.2/13308.md new file mode 100644 index 00000000000000..4d69b8d5d6e531 --- /dev/null +++ b/backport-changelog/7.2/13308.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/13308 + +* https://github.com/WordPress/gutenberg/pull/82175 diff --git a/lib/compat/wordpress-7.2/view-config-api.php b/lib/compat/wordpress-7.2/view-config-api.php index 502316c473141a..0eb33821d6f998 100644 --- a/lib/compat/wordpress-7.2/view-config-api.php +++ b/lib/compat/wordpress-7.2/view-config-api.php @@ -104,6 +104,78 @@ function _gutenberg_get_entity_view_config_posttype_wp_navigation( $data ) { return $data; } +/** + * Post types whose base definition provides its own `form`, without the + * `status` and `discussion` groups of the default post type form. + * + * @var string[] + */ +const GUTENBERG_VIEW_CONFIG_POST_TYPES_WITH_OWN_FORM = array( 'wp_block', 'wp_template', 'wp_template_part' ); + +/** + * Makes the panel summary of the `status` and `discussion` groups explicit in + * the default post type form. + * + * Both groups used to be summarized by the field sharing their id (`status`, + * and the composite `discussion` field). A combined form field no longer + * resolves its own id against the field definitions, so the summary field is + * declared through `layout.summary` instead. The patch merges into the existing + * group members by id, leaving the rest of the form untouched. + * + * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. + * @return Gutenberg_View_Config_Data The updated view configuration container. + */ +function _gutenberg_add_group_summaries_to_default_posttype_form( $data ) { + return $data->merge( + array( + 'form' => array( + 'fields' => array( + array( + 'id' => 'status', + 'layout' => array( + 'type' => 'panel', + 'summary' => 'status', + ), + ), + array( + 'id' => 'discussion', + 'layout' => array( + 'type' => 'panel', + 'summary' => 'discussion', + ), + ), + ), + ), + ), + 1 + ); +} + +/** + * Layers the group summaries on top of the view configuration of every post + * type that uses the default form, including custom post types registered at + * any point. + * + * The callback merges by member id, and a member that is absent would be + * appended instead, so post types whose base definition provides its own form + * are skipped. + * + * @param string $post_type The post type being registered. + */ +function gutenberg_register_default_posttype_form_summaries_7_2( $post_type ) { + if ( in_array( $post_type, GUTENBERG_VIEW_CONFIG_POST_TYPES_WITH_OWN_FORM, true ) ) { + return; + } + + add_filter( + gutenberg_get_entity_view_config_hook_name( 'postType', $post_type ), + '_gutenberg_add_group_summaries_to_default_posttype_form', + 6, + 1 + ); +} +add_action( 'registered_post_type', 'gutenberg_register_default_posttype_form_summaries_7_2' ); + /** * Registers the entity view configuration filters that layer on top of the base * definitions, at a priority between those (5) and third-party callbacks (10), diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index cc623fbb4a630d..82c0385409d22f 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -2,6 +2,37 @@ ## Unreleased +### Breaking Changes + +- DataForm: a combined form field (one with `children`) is now treated purely as a layout container. Its `id` is no longer resolved against the field definitions: a field sharing that `id` no longer contributes validation rules to the group, and the `panel` layout no longer uses it for the collapsed summary or `readOnly` state, falling back to the group's first leaf child instead ([#82175](https://github.com/WordPress/gutenberg/pull/82175)). + + If a combined field relied on sharing its `id` with a field to pick the panel summary, declare it through `layout.summary` instead. For example, a `discussion` field whose `render` summarizes `comment_status` and `ping_status` together: + + ```js + // Before: the summary came from the `discussion` field because the group shares its id. + const form = { + layout: { type: 'panel' }, + fields: [ + { id: 'discussion', children: [ 'comment_status', 'ping_status' ] }, + ], + }; + + // After: the summary field is explicit. Without it, the group would + // now be summarized by its first child, `comment_status`. + const form = { + layout: { type: 'panel' }, + fields: [ + { + id: 'discussion', + layout: { type: 'panel', summary: 'discussion' }, + children: [ 'comment_status', 'ping_status' ], + }, + ], + }; + ``` + + If a combined field relied on a same-id field's `isValid` rules being applied to the group, move those rules to the child fields. + ### Internal - Remove unused dependency `@wordpress/primitives` ([#82103](https://github.com/WordPress/gutenberg/pull/82103)). diff --git a/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts b/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts index b2eed2f9a026df..45195d288fc648 100644 --- a/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts +++ b/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts @@ -11,27 +11,25 @@ const getFieldDefinition = < Item >( field: NormalizedFormField, fields: NormalizedField< Item >[] ) => { - const fieldDefinition = fields.find( ( _field ) => _field.id === field.id ); + // A combined form field is a layout container, not a field: its id is + // never resolved against the field definitions. Fall back to its first + // leaf child so the panel has a definition for the summary and + // `readOnly` state. + if ( !! field.children ) { + const simpleChildren = field.children.filter( + ( child ) => ! child.children + ); - if ( ! fieldDefinition ) { - return fields.find( ( _field ) => { - if ( !! field.children ) { - const simpleChildren = field.children.filter( - ( child ) => ! child.children - ); + if ( simpleChildren.length === 0 ) { + return undefined; + } - if ( simpleChildren.length === 0 ) { - return false; - } - - return _field.id === simpleChildren[ 0 ].id; - } - - return _field.id === field.id; - } ); + return fields.find( + ( _field ) => _field.id === simpleChildren[ 0 ].id + ); } - return fieldDefinition; + return fields.find( ( _field ) => _field.id === field.id ); }; /** @@ -39,8 +37,8 @@ const getFieldDefinition = < Item >( * * Summary fields are determined with the following priority: * 1. Use layout.summary fields if they exist - * 2. Fall back to the field definition that matches the form field's id - * 3. If the form field id doesn't exist, pick the first child field + * 2. For a leaf form field, fall back to its field definition + * 3. For a combined form field, fall back to its first leaf child * 4. If no field definition is found, return empty summary fields * * @param field The form field to get definition for diff --git a/packages/dataviews/src/dataform/stories/layout-panel.tsx b/packages/dataviews/src/dataform/stories/layout-panel.tsx index de0cb5d2ae15ae..a9575197460c16 100644 --- a/packages/dataviews/src/dataform/stories/layout-panel.tsx +++ b/packages/dataviews/src/dataform/stories/layout-panel.tsx @@ -355,6 +355,12 @@ const LayoutPanelComponent = ( { id: 'status', label: 'Status & visibility', children: [ 'status', 'password' ], + layout: getPanelLayoutFromStoryArgs( { + summary: [ 'status' ], + labelPosition, + openAs, + editVisibility, + } ), }, 'order', 'author', @@ -365,11 +371,23 @@ const LayoutPanelComponent = ( { id: 'discussion', label: 'Discussion', children: [ 'comment_status', 'ping_status' ], + layout: getPanelLayoutFromStoryArgs( { + summary: [ 'discussion' ], + labelPosition, + openAs, + editVisibility, + } ), }, { id: 'address1', label: 'Combined address', children: [ 'address1', 'address2', 'city' ], + layout: getPanelLayoutFromStoryArgs( { + summary: [ 'address1' ], + labelPosition, + openAs, + editVisibility, + } ), }, { id: 'flight_info', diff --git a/packages/dataviews/src/hooks/test/use-form-validity.jsdom.test.ts b/packages/dataviews/src/hooks/test/use-form-validity.jsdom.test.ts index 9a9aa1a9507115..b29b84ee47dbb3 100644 --- a/packages/dataviews/src/hooks/test/use-form-validity.jsdom.test.ts +++ b/packages/dataviews/src/hooks/test/use-form-validity.jsdom.test.ts @@ -322,6 +322,44 @@ describe( 'useFormValidity', () => { expect( isValid ).toBe( false ); } ); + // See https://github.com/WordPress/gutenberg/pull/81377#discussion_r3750984349 + it( 'a combined field never resolves its own id against the field definitions', () => { + type Item = { isActive: boolean; a?: string }; + const combinedFields: Field< Item >[] = [ + // Shares the id of the form's combined field. It must be + // ignored: a combined field is a container, not a field, so + // neither its `isValid` nor its `isVisible` apply to the group. + { + id: 'group', + type: 'text', + isVisible: () => false, + isValid: { required: true }, + }, + { + id: 'a', + type: 'text', + isVisible: () => false, + isValid: { required: true }, + }, + ]; + const combinedForm = { + fields: [ { id: 'group', children: [ 'a' ] } ], + }; + const item: Item = { isActive: false, a: undefined }; + const { + result: { + current: { validity, isValid }, + }, + } = renderHook( () => + useFormValidity( item, combinedFields, combinedForm ) + ); + + // The hidden child `a` is skipped and the `group` definition's + // `required` rule is not applied to the combined field. + expect( validity ).toEqual( undefined ); + expect( isValid ).toBe( true ); + } ); + it( 'a stale async result does not resurface after the field is hidden', async () => { let resolveCustom: ( value: string | null ) => void = () => {}; const asyncFields: Field< TestItem >[] = [ diff --git a/packages/dataviews/src/hooks/use-form-validity.ts b/packages/dataviews/src/hooks/use-form-validity.ts index 04e2a8efe01ca9..049548b123701b 100644 --- a/packages/dataviews/src/hooks/use-form-validity.ts +++ b/packages/dataviews/src/hooks/use-form-validity.ts @@ -73,19 +73,10 @@ function getFormFieldsToValidate< Item >( return null; } - const fieldDef = fieldsMap.get( formField.id ); - if ( fieldDef ) { - const [ normalizedField ] = normalizeFields< Item >( [ - fieldDef, - ] ); - - return { - id: formField.id, - children: processedChildren, - field: normalizedField, - } satisfies FormFieldToValidate< Item >; - } - + // A combined form field is a layout container, not a field: + // its id is never resolved against the field definitions, so + // no validation rules are attached to it. Only its children + // are validated. return { id: formField.id, children: processedChildren, diff --git a/phpunit/view-config-api-test.php b/phpunit/view-config-api-test.php index 24323e4b219884..8f6a185e1e33c4 100644 --- a/phpunit/view-config-api-test.php +++ b/phpunit/view-config-api-test.php @@ -71,6 +71,96 @@ public function tear_down() { parent::tear_down(); } + /** + * Returns the form field with the given id from a view configuration. + * + * @param array $config The view configuration. + * @param string $id The form field id. + * @return array|null The form field, or null when absent. + */ + private function get_form_field( $config, $id ) { + foreach ( $config['form']['fields'] as $field ) { + if ( is_array( $field ) && isset( $field['id'] ) && $id === $field['id'] ) { + return $field; + } + } + return null; + } + + /** + * The `status` and `discussion` groups of the default post type form declare + * their panel summary explicitly, for built-in and custom post types alike. + * + * @dataProvider data_default_form_post_types + * + * @param string $post_type The post type. + */ + public function test_default_form_groups_declare_summary( $post_type ) { + if ( ! post_type_exists( $post_type ) ) { + register_post_type( $post_type ); + } + + $config = gutenberg_get_entity_view_config( 'postType', $post_type ); + + foreach ( array( 'status', 'discussion' ) as $group ) { + $field = $this->get_form_field( $config, $group ); + $this->assertNotNull( $field, "The `{$group}` group is present." ); + $this->assertNotEmpty( $field['children'], "The `{$group}` group keeps its children." ); + $this->assertSame( + array( + 'type' => 'panel', + 'summary' => $group, + ), + $field['layout'], + "The `{$group}` group summary is explicit." + ); + } + + if ( 'page' !== $post_type && 'post' !== $post_type ) { + unregister_post_type( $post_type ); + } + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_default_form_post_types() { + return array( + 'page' => array( 'page' ), + 'post' => array( 'post' ), + 'custom post type' => array( 'summaries_cpt' ), + ); + } + + /** + * Post types with their own form do not receive the default form groups. + * + * @dataProvider data_post_types_with_own_form + * + * @param string $post_type The post type. + */ + public function test_own_form_post_types_do_not_get_default_groups( $post_type ) { + $config = gutenberg_get_entity_view_config( 'postType', $post_type ); + + $this->assertNull( $this->get_form_field( $config, 'status' ) ); + $this->assertNull( $this->get_form_field( $config, 'discussion' ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_post_types_with_own_form() { + return array( + 'wp_block' => array( 'wp_block' ), + 'wp_template' => array( 'wp_template' ), + 'wp_template_part' => array( 'wp_template_part' ), + ); + } + /** * The default configuration exposes the documented shape for an unknown entity. */ diff --git a/routes/post-list/quick-edit-modal.tsx b/routes/post-list/quick-edit-modal.tsx index 38b84d4853a53d..2b57043895c36e 100644 --- a/routes/post-list/quick-edit-modal.tsx +++ b/routes/post-list/quick-edit-modal.tsx @@ -154,6 +154,10 @@ export function QuickEditModal( { { id: 'status', label: __( 'Status' ), + layout: { + type: 'panel', + summary: 'status', + }, children: [ { id: 'status', @@ -170,6 +174,10 @@ export function QuickEditModal( { { id: 'discussion', label: __( 'Discussion' ), + layout: { + type: 'panel', + summary: 'discussion', + }, children: [ { id: 'comment_status', diff --git a/test/e2e/specs/site-editor/page-list.spec.js b/test/e2e/specs/site-editor/page-list.spec.js index 5b4f6f26f6d090..17e3313c30674c 100644 --- a/test/e2e/specs/site-editor/page-list.spec.js +++ b/test/e2e/specs/site-editor/page-list.spec.js @@ -69,6 +69,13 @@ test.describe( 'Page List', () => { const TEST_IMAGE_FILE_PATH = './assets/10x10_e2e_test_image_z9T8jK.png'; + // The media modal opens on the "Media Library" tab once + // the library has items (e.g. after a previous upload), + // so make sure the upload tab is the active one. + await mediaLibrary + .getByRole( 'tab', { name: 'Upload files' } ) + .click(); + const fileChooserPromise = page.waitForEvent( 'filechooser' ); await mediaLibrary.getByText( 'Select files' ).click();