-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Allow plugins to manipulate template switching #63284
Description
What problem does this address?
Currently, the useAllowSwitchingTemplates is used to disable template swapping and creating for static home and blog pages. This prevents users from accidentally creating or swapping templates but still gives them information about the connected template and a path to edit it from the post editor, which is a nice UX.
Other plugins can benefit from this hook. For example, WooCommerce has Product Catalog, Cart, and Checkout page that uses fixed templates like the blog page.
gutenberg/packages/editor/src/components/post-template/hooks.js
Lines 22 to 42 in e1e4967
| export function useAllowSwitchingTemplates() { | |
| const { postType, postId } = useEditedPostContext(); | |
| return useSelect( | |
| ( select ) => { | |
| const { getEntityRecord, getEntityRecords } = select( coreStore ); | |
| const siteSettings = getEntityRecord( 'root', 'site' ); | |
| const templates = getEntityRecords( 'postType', 'wp_template', { | |
| per_page: -1, | |
| } ); | |
| const isPostsPage = +postId === siteSettings?.page_for_posts; | |
| // If current page is set front page or posts page, we also need | |
| // to check if the current theme has a template for it. If not | |
| const isFrontPage = | |
| postType === 'page' && | |
| +postId === siteSettings?.page_on_front && | |
| templates?.some( ( { slug } ) => slug === 'front-page' ); | |
| return ! isPostsPage && ! isFrontPage; | |
| }, | |
| [ postId, postType ] | |
| ); | |
| } |
The current hook only supports static home and blog page.
What is your proposed solution?
It'd be awesome to have a mechanism enabling plugins to manipulate the template-switching ability of the current editing page.