Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Show default template name on template picker list. #45865

Conversation

jorgefilipecosta
Copy link
Member

Fixes: #41449

This PR updates the default template label to say what the default template name is. This makes the user aware of what the default template is.

What?

The implementation was a little complex as we needed to compute the default template for the post if it had no template assigned to it. I followed the logic in get_single_template, get_page_template, and get_singular_template.

Screenshots or screencast

image
image

@codesandbox
Copy link

codesandbox bot commented Nov 17, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@skorasaurus
Copy link
Member

skorasaurus commented Nov 17, 2022

Thanks for your effort on this; based on reading over the code, I'm assuming this is only applicable for Block-based/Full site themes?

I tested the PR on a couple hybrid themes (Ernesto) and twentytwenty (with a theme.json included) and the default label says 'default template'

image

ref: #45216

@Mamaduka Mamaduka self-requested a review November 18, 2022 05:07
if ( $post_type === 'page' ) {
$templates = array();
if ( ! empty( $post->post_name ) ) {
$templates[] = "page-{$post->post_name}.php";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$templates[] = "page-{$post->post_name}.php";
$templates[] = "page-{$post->post_name}";

I think we should drop the php suffix for block templates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feedback was addressed 👍

@jorgefilipecosta jorgefilipecosta force-pushed the update/show-default-template-name-on-template-picker-list branch 2 times, most recently from 7b7776c to a295b3e Compare November 18, 2022 15:14
@jameskoster
Copy link
Contributor

I can't comment on the code, but just leaving a note to confirm that this is working as expected for me for block templates.

@jorgefilipecosta jorgefilipecosta force-pushed the update/show-default-template-name-on-template-picker-list branch from a295b3e to aa287a9 Compare December 5, 2022 14:22
@jorgefilipecosta jorgefilipecosta force-pushed the update/show-default-template-name-on-template-picker-list branch from aa287a9 to 5837b7a Compare December 5, 2022 17:40
@jorgefilipecosta
Copy link
Member Author

Thanks for your effort on this; based on reading over the code, I'm assuming this is only applicable for Block-based/Full site themes?

I tested the PR on a couple hybrid themes (Ernesto) and twentytwenty (with a theme.json included) and the default label says 'default template'

image

ref: #45216
Hi @skorasaurus, yes, that's applicable only for block-based themes where we have a template object with names, etc.

@jorgefilipecosta jorgefilipecosta added the Needs PHP backport Needs PHP backport to Core label Dec 6, 2022
}
return $label;
}
add_filter( 'default_page_template_title', 'gutenberg_add_template_to_default_template_title' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been checking, Core code, and this filter is used in a two other places: some kind of meta box, and the post list. So I'm guessing this change will impact these places as well. Do we want to do that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. We want to do that. This only affects block themes, and we want in post list and other places to show the same template title as in the post editor.

* @return string The new display value for the default page template title.
*/
function gutenberg_add_template_to_default_template_title( $label, $context = '' ) {
global $post_type, $post;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, if I'm understanding properly, we're trying to run WordPress's template resolution algorithm for the current global $post and $post_type. I feel this is not the first time I make this comment but it would be great if this algorithm could be abstracted in a dependency-less class/function. Is there a way to share code? (Probably not for this PR anyway)

Also, I want to ping @ockham here as I believe he worked on this kind of things previously for block templates resolution.

Also, this will run the template resolution for each editor page load, I'm assuming that's fast enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be great if this algorithm could be abstracted in a dependency-less class/function.

100%. It would be great to have something similar to Hierarchy in the core. A class that can resolve a template based on the current post or query context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, if I'm understanding properly, we're trying to run WordPress's template resolution algorithm for the current global $post and $post_type. I feel this is not the first time I make this comment but it would be great if this algorithm could be abstracted in a dependency-less class/function. Is there a way to share code? (Probably not for this PR anyway)

Sure, I agree that abstracting template resolution is a common need. However, it's not straightforward to implement. For example, in this case, we want template resolution to use the default/fallback template even if a custom template has been assigned. In other cases, we may need template resolution to work differently.

I don't think this should be a blocker for us. We can explore options for abstraction while also addressing the specific needs we have, and later consolidate.

Also, this will run the template resolution for each editor page load, I'm assuming that's fast enough?

Yes, I think it is something fast enough we also call these functions on the site front end anyway, where performance is even more critical.

@youknowriad
Copy link
Contributor

I'm not sure I like this PR personally, I think it's adding complexity and a bit of fragility (for instance the template name doesn't change after you save or make changes to a post which could impact its template, like assigning an id...)

@youknowriad
Copy link
Contributor

I'd be more ok with this change if we had the right abstraction in place and my thinking is that maybe it's not worth it with a hacky implementation. I know that we can't do better than this for now though.

@ramonjd ramonjd added [Feature] Templates API Related to API powering block template functionality in the Site Editor and removed Needs PHP backport Needs PHP backport to Core labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template panel: "Default template" can be a confusing option label
6 participants