How can I add 'view mode' to screen options for pages and cpts?

The ‘screen options’ for posts has a view mode option, enabling switching between ‘list’ and ‘excerpt’ view (in which for the latter the except appears below the title).

How can I add such an option to pages (support for excerpts is enabled) and cpts which support excerpts?

Solutions Collecting From Web of "How can I add 'view mode' to screen options for pages and cpts?"

Currently the excerpt view is limited to non-hierarchical post types.

We are able to filter the view mode in the Screen Options:

view mode

with the view_mode_post_types filter, introduced within ticket #22222 in changeset #35357:

/**
 * Filters the post types that have different view mode options.
 *
 * @since 4.4.0
 *
 * @param array $view_mode_post_types Array of post types that can change view modes.
 *                                    Default hierarchical post types with show_ui on.
 */
$view_mode_post_types = apply_filters( 'view_mode_post_types', $view_mode_post_types );

but because of this check in WP_Posts_List_Table::column_title():

if ( 
       ! is_post_type_hierarchical( $this->screen->post_type ) 
    && 'excerpt' === $mode 
    && current_user_can( 'read_post', $post->ID ) 
) {
     echo esc_html( get_the_excerpt() );
}

we’re not able to modify this check directly to add support for hierarchical post types.

There are some hackish workarounds possible but I didn’t explore it further, as the ones I have in mind, might not be stable here.

ps: I think there’s a typo in the inline documentation:

Default hierarchical post types with show_ui on.

should be

Default non-hierarchical post types with show_ui on.

because the default ones are src:

$view_mode_post_types = get_post_types( 
    array( 'hierarchical' => false, 'show_ui' => true ) 
);

Update: The documentation for the filter has been fixed by ticket #41730