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

Page Inspector Swap Template Functionally has broken the page template hierarchy #54596

Closed
mikejolley opened this issue Sep 19, 2023 · 2 comments
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Type] Bug An existing feature does not function as intended

Comments

@mikejolley
Copy link
Contributor

Description

This PR merged last week introduced the ability to "swap templates" for pages. However, something in this PR has broken the page template hierarchy, meaning in the site editor only, it claims the "Pages" template is in use, when in fact a custom template exists.

As an example, in this case there is a template called page-cart. This is also registered via the page_template_hierarchy filter to ensure it has priority over all other templates. Before #51477 this is what you would see in the page editor:

Screenshot 2023-09-19 at 12 29 34

After #51477 you can see it now uses the generic "pages" template instead:

Screenshot 2023-09-19 at 12 32 35

Whats more, the preview in the site editor also uses the "pages" template. The frontend views still use page-cart as before.

It seems as though the issues are:

  1. Hierarchy is no longer respected in the site editor
  2. Swap template functionality should not be available if a custom template is in place

Step-by-step reproduction instructions

We're running into this issue with WooCommerce Blocks which includes a cart template which should be the default.

  • Install latest WooCommerce
  • Checkout and build blocks from trunk https://github.com/woocommerce/woocommerce-blocks. nvm use && npm install && npm run build
  • Activate both and setup as normal. After setup you should have both a "cart" and "checkout" page under pages.
  • Go to Appearance > Editor > Pages > Cart. See in the inspector it says the template being used is "Pages". Notice the preview uses the "pages" template
  • Checkout gutenberg with SHA 7d054a2. This is before [Site Editor - Page Inspector]: Add ability to switch templates #51477
  • Go to Appearance > Editor > Pages > Cart. See in the inspector it says the template being used is "Page: Cart". Notice the preview uses the "Page: Cart" template (no comments or featured image is visible).

Screenshots, screen recording, code snippet

No response

Environment info

  • Latest Gutenberg from trunk
  • Twenty Twenty Three

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

No

@mikejolley
Copy link
Contributor Author

The problem seems to be related to the if ( page.path ) { logic here:

https://github.com/WordPress/gutenberg/pull/51477/files#diff-2df53ac7ebda88cf7debc8c129460578d46471090bceb7f512cd568728a48f73R268

In my case, this comes back as undefined. I'm assuming it should be /cart since thats the page permalink. If I hardcode /cart there the correct template is restored.

@mikejolley
Copy link
Contributor Author

Ah look, it introduced a method called getDefaultTemplate. This expects a slug.

const getDefaultTemplate = async ( slug ) =>
			apiFetch( {
				path: addQueryArgs( '/wp/v2/templates/lookup', {
					slug: `page-${ slug }`,
				} ),
			} );

But we're not passing a slug. We're passing the link: https://github.com/WordPress/gutenberg/pull/51477/files#diff-2df53ac7ebda88cf7debc8c129460578d46471090bceb7f512cd568728a48f73R298

Looking at the requests:

Screenshot 2023-09-19 at 12 58 21

Ill keep digging.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") labels Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Type] Bug An existing feature does not function as intended
3 participants