Skip to content

Commit

Permalink
Site Editor: fix template for page-on-front option (WordPress#66739)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: draganescu <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: mdawaffe <[email protected]>
  • Loading branch information
5 people authored Nov 5, 2024
1 parent 6cfeadc commit e6ee784
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,32 @@ function useResolveEditedEntityAndContext( { postId, postType } ) {
postTypeToResolve === 'page' &&
homepageId === postIdToResolve
) {
return getDefaultTemplateId( { slug: 'front-page' } );
// The /lookup endpoint cannot currently handle a lookup
// when a page is set as the front page, so specifically in
// that case, we want to check if there is a front page
// template, and instead of falling back to the home
// template, we want to fall back to the page template.
const templates = getEntityRecords(
'postType',
TEMPLATE_POST_TYPE,
{
per_page: -1,
}
);
if ( templates ) {
const id = templates?.find(
( { slug } ) => slug === 'front-page'
)?.id;
if ( id ) {
return id;
}

// If no front page template is found, continue with the
// logic below (fetching the page template).
} else {
// Still resolving `templates`.
return undefined;
}
}

const editedEntity = getEditedEntityRecord(
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/specs/site-editor/template-hierarchy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Template hierarchy', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyfour' );
} );

test.afterEach( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );

test( 'shows correct template with page on front option', async ( {
admin,
page,
editor,
} ) => {
await admin.visitAdminPage( 'options-reading.php' );
await page.click( 'input[name="show_on_front"][value="page"]' );
await page.selectOption( 'select[name="page_on_front"]', '2' );
await page.click( 'input[type="submit"]' );
await admin.visitSiteEditor();

// Title block should contain "Sample Page"
await expect(
editor.canvas.locator( 'role=document[name="Block: Title"]' )
).toContainText( 'Sample Page' );

await admin.visitAdminPage( 'options-reading.php' );
await page.click( 'input[name="show_on_front"][value="posts"]' );
await page.click( 'input[type="submit"]' );
} );
} );

0 comments on commit e6ee784

Please sign in to comment.