Skip to content

Commit d7b4ec5

Browse files
Editor: Update the entity view configs for the server fields.
Adds the view configs of the `attachment` post type, whose form is the media editor form, and of the `wp_navigation` post type, which lists navigation menus by date. The `attachment` form replaces the default post type form, so the media editor no longer shows only the date and author. Synced from WordPress/gutenberg#84700. See #66229.
1 parent 7e916f2 commit d7b4ec5

4 files changed

Lines changed: 231 additions & 1 deletion

File tree

‎src/wp-includes/default-filters.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@
841841
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
842842

843843
// View Config API.
844-
foreach ( array( 'page', 'wp_block', 'wp_template_part', 'wp_template' ) as $post_type ) {
844+
foreach ( array( 'page', 'wp_block', 'wp_template_part', 'wp_template', 'wp_navigation', 'attachment' ) as $post_type ) {
845845
// Base definitions run before the default priority, so third-party
846846
// callbacks registered at the default compose on top of them
847847
// regardless of registration order.

‎src/wp-includes/view-config.php‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,99 @@ function _wp_get_entity_view_config_posttype_wp_template( $data ) {
771771
return $data;
772772
}
773773

774+
/**
775+
* Provides the view configuration for the `wp_navigation` post type.
776+
*
777+
* @since 7.2.0
778+
*
779+
* @param WP_View_Config_Data $data The view configuration container for the entity.
780+
* @return WP_View_Config_Data The updated view configuration container.
781+
*/
782+
function _wp_get_entity_view_config_posttype_wp_navigation( $data ) {
783+
$default_layouts = array(
784+
'list' => array(),
785+
);
786+
787+
$default_view = array(
788+
'type' => 'list',
789+
'filters' => array(),
790+
'perPage' => 20,
791+
'sort' => array(
792+
'field' => 'date',
793+
'direction' => 'desc',
794+
),
795+
'titleField' => 'title',
796+
'fields' => array(),
797+
);
798+
799+
// The base config already provides the "All" view titled with the post
800+
// type's `all_items` label, so only the default view and layouts change.
801+
$data->set(
802+
array(
803+
'default_view' => $default_view,
804+
'default_layouts' => $default_layouts,
805+
),
806+
1
807+
);
808+
809+
return $data;
810+
}
811+
812+
/**
813+
* Provides the view configuration for the `attachment` post type.
814+
*
815+
* An attachment is edited through a form (the media editor) that shares
816+
* nothing with the default post type form, so the `form` is replaced: the
817+
* editable fields first, in a regular layout, then the metadata and the file
818+
* information, in panels. The generic `default_view`, `default_layouts`, and
819+
* `view_list` built by wp_get_entity_view_config() are left untouched.
820+
*
821+
* @since 7.2.0
822+
*
823+
* @param WP_View_Config_Data $data The view configuration container for the entity.
824+
* @return WP_View_Config_Data The updated view configuration container.
825+
*/
826+
function _wp_get_entity_view_config_posttype_attachment( $data ) {
827+
$regular_layout = array(
828+
'type' => 'regular',
829+
'labelPosition' => 'top',
830+
);
831+
832+
return $data->set(
833+
array(
834+
'form' => array(
835+
'layout' => array( 'type' => 'panel' ),
836+
'fields' => array(
837+
array(
838+
'id' => 'title',
839+
'layout' => $regular_layout,
840+
),
841+
array(
842+
'id' => 'alt_text',
843+
'layout' => $regular_layout,
844+
),
845+
array(
846+
'id' => 'caption',
847+
'layout' => $regular_layout,
848+
),
849+
array(
850+
'id' => 'description',
851+
'layout' => $regular_layout,
852+
),
853+
'date',
854+
'author',
855+
'filename',
856+
'mime_type',
857+
'filesize',
858+
'media_dimensions',
859+
'attached_to',
860+
),
861+
),
862+
),
863+
1
864+
);
865+
}
866+
774867
/**
775868
* Provides the view configuration for the `root`/`site` entity.
776869
*

‎tests/phpunit/tests/rest-api/rest-view-config-controller.php‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,65 @@ public function test_get_items_matches_underlying_config() {
302302
$this->assertSame( $config['form'], $data['form'] );
303303
}
304304

305+
/**
306+
* The `wp_navigation` post type provides its own list-based default view.
307+
*
308+
* @ticket 66229
309+
*
310+
* @covers ::get_items
311+
*/
312+
public function test_get_items_wp_navigation_default_view() {
313+
// Admin: reading wp_navigation config requires `edit_theme_options`.
314+
wp_set_current_user( self::$admin_id );
315+
316+
$response = $this->dispatch_request( 'postType', 'wp_navigation' );
317+
$this->assertSame( 200, $response->get_status(), 'The request succeeds.' );
318+
319+
$data = json_decode( wp_json_encode( $response->get_data() ), true );
320+
321+
$this->assertSame( 'list', $data['default_view']['type'], 'The default view uses the list layout.' );
322+
$this->assertSame(
323+
array(
324+
'field' => 'date',
325+
'direction' => 'desc',
326+
),
327+
$data['default_view']['sort'],
328+
'The default view sorts by date, newest first.'
329+
);
330+
$this->assertSame( 'title', $data['default_view']['titleField'], 'The default view uses the title field.' );
331+
$this->assertSame( array( 'list' ), array_keys( $data['default_layouts'] ), 'Only the list layout is available.' );
332+
$this->assertCount( 1, $data['view_list'], 'The view list has a single view.' );
333+
$this->assertSame( 'all', $data['view_list'][0]['slug'], 'The single view is the "All" view.' );
334+
}
335+
336+
/**
337+
* The `attachment` post type provides the form of the media editor.
338+
*
339+
* @ticket 66229
340+
*
341+
* @covers ::get_items
342+
*/
343+
public function test_get_items_attachment_form() {
344+
wp_set_current_user( self::$editor_id );
345+
346+
$response = $this->dispatch_request( 'postType', 'attachment' );
347+
$this->assertSame( 200, $response->get_status(), 'The request succeeds.' );
348+
349+
$data = json_decode( wp_json_encode( $response->get_data() ), true );
350+
351+
$this->assertSame( array( 'type' => 'panel' ), $data['form']['layout'], 'The form uses a panel layout.' );
352+
$this->assertSame(
353+
array( 'title', 'alt_text', 'caption', 'description', 'date', 'author', 'filename', 'mime_type', 'filesize', 'media_dimensions', 'attached_to' ),
354+
array_map(
355+
static function ( $field ) {
356+
return is_array( $field ) ? $field['id'] : $field;
357+
},
358+
$data['form']['fields']
359+
),
360+
'The form lists the media editor fields in order.'
361+
);
362+
}
363+
305364
/**
306365
* The `root`/`site` entity provides the form of the site identity screen.
307366
*

‎tests/phpunit/tests/view-config.php‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,84 @@ public function data_default_form_post_types() {
139139
);
140140
}
141141

142+
/**
143+
* Post types with their own form do not receive the default form groups.
144+
*
145+
* @ticket 66229
146+
*
147+
* @dataProvider data_post_types_with_own_form
148+
*
149+
* @param string $post_type The post type.
150+
*/
151+
public function test_own_form_post_types_do_not_get_default_groups( $post_type ) {
152+
$config = wp_get_entity_view_config( 'postType', $post_type );
153+
154+
$this->assertNull( $this->get_form_field( $config, 'status' ), 'The `status` group is absent.' );
155+
$this->assertNull( $this->get_form_field( $config, 'discussion' ), 'The `discussion` group is absent.' );
156+
}
157+
158+
/**
159+
* Data provider.
160+
*
161+
* @return array[]
162+
*/
163+
public function data_post_types_with_own_form() {
164+
return array(
165+
'attachment' => array( 'attachment' ),
166+
'wp_block' => array( 'wp_block' ),
167+
'wp_template' => array( 'wp_template' ),
168+
'wp_template_part' => array( 'wp_template_part' ),
169+
);
170+
}
171+
172+
/**
173+
* The `attachment` post type provides the form of the media editor: the
174+
* editable fields in a regular layout, then the rest in panels.
175+
*
176+
* @ticket 66229
177+
*
178+
* @covers ::_wp_get_entity_view_config_posttype_attachment
179+
*/
180+
public function test_attachment_form() {
181+
$config = wp_get_entity_view_config( 'postType', 'attachment' );
182+
183+
$this->assertSame( array( 'type' => 'panel' ), $config['form']['layout'], 'The form uses a panel layout.' );
184+
185+
$regular_layout = array(
186+
'type' => 'regular',
187+
'labelPosition' => 'top',
188+
);
189+
$this->assertSame(
190+
array(
191+
array(
192+
'id' => 'title',
193+
'layout' => $regular_layout,
194+
),
195+
array(
196+
'id' => 'alt_text',
197+
'layout' => $regular_layout,
198+
),
199+
array(
200+
'id' => 'caption',
201+
'layout' => $regular_layout,
202+
),
203+
array(
204+
'id' => 'description',
205+
'layout' => $regular_layout,
206+
),
207+
'date',
208+
'author',
209+
'filename',
210+
'mime_type',
211+
'filesize',
212+
'media_dimensions',
213+
'attached_to',
214+
),
215+
$config['form']['fields'],
216+
'The form lists the media editor fields in order.'
217+
);
218+
}
219+
142220
/**
143221
* The default configuration exposes the documented shape for an unknown entity.
144222
*/

0 commit comments

Comments
 (0)