Skip to content

Commit

Permalink
Exclude default templates when requesting templates for post type
Browse files Browse the repository at this point in the history
Introduces 'WP_Block_Template::$default' property
  • Loading branch information
Mamaduka committed Oct 21, 2021
1 parent d91a80e commit be7686b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t
$template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];
$template->status = 'publish';
$template->has_theme_file = true;
$template->default = false;

if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
$template->default = true;
}

if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) {
Expand Down Expand Up @@ -304,6 +306,7 @@ function _gutenberg_build_template_result_from_post( $post ) {
$template->title = $post->post_title;
$template->status = $post->post_status;
$template->has_theme_file = $has_theme_file;
$template->default = false;

This comment has been minimized.

Copy link
@youknowriad

youknowriad Oct 21, 2021

Contributor

Why are all saved templates marked as "default" false, I mean If I save a template called "index", it's still the "index" template from the hierarchy and it shouldn't show in the page template selector right?

Also it feels as "default" doesn't provide the right semantics, it's hard to understand its purpose, should we find another name for this flag?

This comment has been minimized.

Copy link
@Mamaduka

Mamaduka Oct 21, 2021

Author Member

Thanks for pointing this out. It makes sense to me. I will update the logic.

I used "default" as a name because it's how we call them in gutenberg_get_default_template_types. What do you think about "core" instead of "default"?

Naming things is hard in the mornings 😄

This comment has been minimized.

Copy link
@youknowriad

youknowriad Oct 21, 2021

Contributor

Yeah, not my best skill either. I'd personally be more explicit, something like "is-in-hierarchy" or the opposite "is-custom-page-template". Any thoughts @jasmussen maybe? How do we differentiate between a template that should show up in the pate template selector in the post editor (custom templates we can assign to pages and posts) and the ones that shouldn't not (templates inherited automatically from the template hierarchy)?

This comment has been minimized.

Copy link
@jasmussen

jasmussen Oct 21, 2021

Contributor

Do you mean like how I shouldn't be able to assign "Archive" as a template in this dropdown?
Screenshot 2021-10-21 at 10 50 15

If that's right, then anything that includes "page template" feels the most right to me, simply based on history. Did I miss any nuance here?

This comment has been minimized.

Copy link
@Mamaduka

Mamaduka Oct 21, 2021

Author Member

Let's go with is_custom for now. We can change it later before merging in the core.

P.S. I'm going to remove the template suffix since the property is already in the WP_Block_Template context.


if ( 'wp_template_part' === $post->post_type ) {
$type_terms = get_the_terms( $post, 'wp_template_part_area' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function get_items( $request ) {

$templates = array();
foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) {
// Maybe we should do this on `get_block_templates` level based on query?
if ( isset( $request['post_type'] ) && $template->default ) {
continue;
}

if (
isset( $request['post_type'] ) &&
isset( $template->post_types ) &&
Expand Down
7 changes: 7 additions & 0 deletions lib/full-site-editing/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ class WP_Block_Template {
* @var boolean
*/
public $has_theme_file;

/**
* Whether a template is default WordPress template (e.g. 'index', 'archive').
*
* @var bool
*/
public $default = false;
}
4 changes: 4 additions & 0 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_t

$block_templates = gutenberg_get_block_templates( array(), 'wp_template' );
foreach ( $block_templates as $template ) {
if ( $template->default ) {
continue;
}

if ( isset( $template->post_types ) && ! in_array( $post_type, $template->post_types, true ) ) {
continue;
}
Expand Down

0 comments on commit be7686b

Please sign in to comment.