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

Site Editor: Template parts not loading when block theme has parent directory #27633

Closed
wants to merge 5 commits into from

Conversation

jeyip
Copy link
Contributor

@jeyip jeyip commented Dec 10, 2020

Description

After latest Gutenberg plugin updates (v9.5.1), template parts are no longer loading properly when using themes that have a parent directory

Cause

How has this been tested?

  1. Create a WordPress instance where block themes live within a parent directory
  2. Spin up the WordPress instance with Gutenberg
  3. Enable the block theme
  4. Navigate to the site editor
  5. Ensure that template parts load

Screenshots

Before

Screenshot 2020-12-08 at 23 57 51

Types of changes

Bug fix for #48145

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@jeyip jeyip added [Type] Bug An existing feature does not function as intended [Feature] Full Site Editing labels Dec 10, 2020
@jeyip jeyip self-assigned this Dec 10, 2020
@github-actions
Copy link

github-actions bot commented Dec 10, 2020

Size Change: +14 B (0%)

Total Size: 1.28 MB

Filename Size Change
build/block-library/index.js 150 kB +14 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.81 kB 0 B
build/api-fetch/index.js 3.42 kB 0 B
build/autop/index.js 2.83 kB 0 B
build/blob/index.js 664 B 0 B
build/block-directory/index.js 8.71 kB 0 B
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/index.js 129 kB 0 B
build/block-editor/style-rtl.css 11.2 kB 0 B
build/block-editor/style.css 11.2 kB 0 B
build/block-library/blocks/archives/editor-rtl.css 120 B 0 B
build/block-library/blocks/archives/editor.css 119 B 0 B
build/block-library/blocks/audio/editor-rtl.css 118 B 0 B
build/block-library/blocks/audio/editor.css 118 B 0 B
build/block-library/blocks/audio/style-rtl.css 152 B 0 B
build/block-library/blocks/audio/style.css 152 B 0 B
build/block-library/blocks/block/editor-rtl.css 211 B 0 B
build/block-library/blocks/block/editor.css 211 B 0 B
build/block-library/blocks/button/editor-rtl.css 513 B 0 B
build/block-library/blocks/button/editor.css 513 B 0 B
build/block-library/blocks/button/style-rtl.css 488 B 0 B
build/block-library/blocks/button/style.css 488 B 0 B
build/block-library/blocks/buttons/editor-rtl.css 275 B 0 B
build/block-library/blocks/buttons/editor.css 275 B 0 B
build/block-library/blocks/buttons/style-rtl.css 346 B 0 B
build/block-library/blocks/buttons/style.css 346 B 0 B
build/block-library/blocks/calendar/style-rtl.css 249 B 0 B
build/block-library/blocks/calendar/style.css 249 B 0 B
build/block-library/blocks/categories/editor-rtl.css 135 B 0 B
build/block-library/blocks/categories/editor.css 135 B 0 B
build/block-library/blocks/categories/style-rtl.css 132 B 0 B
build/block-library/blocks/categories/style.css 132 B 0 B
build/block-library/blocks/code/style-rtl.css 152 B 0 B
build/block-library/blocks/code/style.css 152 B 0 B
build/block-library/blocks/columns/editor-rtl.css 239 B 0 B
build/block-library/blocks/columns/editor.css 239 B 0 B
build/block-library/blocks/columns/style-rtl.css 467 B 0 B
build/block-library/blocks/columns/style.css 466 B 0 B
build/block-library/blocks/cover/editor-rtl.css 440 B 0 B
build/block-library/blocks/cover/editor.css 438 B 0 B
build/block-library/blocks/cover/style-rtl.css 1.27 kB 0 B
build/block-library/blocks/cover/style.css 1.27 kB 0 B
build/block-library/blocks/embed/editor-rtl.css 529 B 0 B
build/block-library/blocks/embed/editor.css 529 B 0 B
build/block-library/blocks/embed/style-rtl.css 419 B 0 B
build/block-library/blocks/embed/style.css 419 B 0 B
build/block-library/blocks/file/editor-rtl.css 246 B 0 B
build/block-library/blocks/file/editor.css 245 B 0 B
build/block-library/blocks/file/style-rtl.css 288 B 0 B
build/block-library/blocks/file/style.css 289 B 0 B
build/block-library/blocks/freeform/editor-rtl.css 2.49 kB 0 B
build/block-library/blocks/freeform/editor.css 2.49 kB 0 B
build/block-library/blocks/gallery/editor-rtl.css 692 B 0 B
build/block-library/blocks/gallery/editor.css 693 B 0 B
build/block-library/blocks/gallery/style-rtl.css 1.11 kB 0 B
build/block-library/blocks/gallery/style.css 1.11 kB 0 B
build/block-library/blocks/group/editor-rtl.css 364 B 0 B
build/block-library/blocks/group/editor.css 364 B 0 B
build/block-library/blocks/group/style-rtl.css 117 B 0 B
build/block-library/blocks/group/style.css 117 B 0 B
build/block-library/blocks/heading/editor-rtl.css 174 B 0 B
build/block-library/blocks/heading/editor.css 174 B 0 B
build/block-library/blocks/heading/style-rtl.css 137 B 0 B
build/block-library/blocks/heading/style.css 137 B 0 B
build/block-library/blocks/html/editor-rtl.css 324 B 0 B
build/block-library/blocks/html/editor.css 325 B 0 B
build/block-library/blocks/image/editor-rtl.css 738 B 0 B
build/block-library/blocks/image/editor.css 737 B 0 B
build/block-library/blocks/image/style-rtl.css 510 B 0 B
build/block-library/blocks/image/style.css 511 B 0 B
build/block-library/blocks/latest-comments/editor-rtl.css 201 B 0 B
build/block-library/blocks/latest-comments/editor.css 200 B 0 B
build/block-library/blocks/latest-comments/style-rtl.css 315 B 0 B
build/block-library/blocks/latest-comments/style.css 315 B 0 B
build/block-library/blocks/latest-posts/editor-rtl.css 183 B 0 B
build/block-library/blocks/latest-posts/editor.css 183 B 0 B
build/block-library/blocks/latest-posts/style-rtl.css 568 B 0 B
build/block-library/blocks/latest-posts/style.css 567 B 0 B
build/block-library/blocks/list/editor-rtl.css 129 B 0 B
build/block-library/blocks/list/editor.css 129 B 0 B
build/block-library/blocks/list/style-rtl.css 127 B 0 B
build/block-library/blocks/list/style.css 127 B 0 B
build/block-library/blocks/media-text/editor-rtl.css 240 B 0 B
build/block-library/blocks/media-text/editor.css 240 B 0 B
build/block-library/blocks/media-text/style-rtl.css 579 B 0 B
build/block-library/blocks/media-text/style.css 577 B 0 B
build/block-library/blocks/more/editor-rtl.css 479 B 0 B
build/block-library/blocks/more/editor.css 479 B 0 B
build/block-library/blocks/navigation-link/editor-rtl.css 438 B 0 B
build/block-library/blocks/navigation-link/editor.css 440 B 0 B
build/block-library/blocks/navigation-link/style-rtl.css 747 B 0 B
build/block-library/blocks/navigation-link/style.css 745 B 0 B
build/block-library/blocks/navigation/editor-rtl.css 1.31 kB 0 B
build/block-library/blocks/navigation/editor.css 1.31 kB 0 B
build/block-library/blocks/navigation/style-rtl.css 222 B 0 B
build/block-library/blocks/navigation/style.css 222 B 0 B
build/block-library/blocks/nextpage/editor-rtl.css 440 B 0 B
build/block-library/blocks/nextpage/editor.css 440 B 0 B
build/block-library/blocks/paragraph/editor-rtl.css 161 B 0 B
build/block-library/blocks/paragraph/editor.css 161 B 0 B
build/block-library/blocks/paragraph/style-rtl.css 279 B 0 B
build/block-library/blocks/paragraph/style.css 279 B 0 B
build/block-library/blocks/post-author/editor-rtl.css 255 B 0 B
build/block-library/blocks/post-author/editor.css 255 B 0 B
build/block-library/blocks/post-author/style-rtl.css 229 B 0 B
build/block-library/blocks/post-author/style.css 230 B 0 B
build/block-library/blocks/post-comments-form/style-rtl.css 293 B 0 B
build/block-library/blocks/post-comments-form/style.css 293 B 0 B
build/block-library/blocks/post-content/editor-rtl.css 187 B 0 B
build/block-library/blocks/post-content/editor.css 187 B 0 B
build/block-library/blocks/post-excerpt/editor-rtl.css 134 B 0 B
build/block-library/blocks/post-excerpt/editor.css 134 B 0 B
build/block-library/blocks/post-featured-image/editor-rtl.css 387 B 0 B
build/block-library/blocks/post-featured-image/editor.css 386 B 0 B
build/block-library/blocks/post-featured-image/style-rtl.css 149 B 0 B
build/block-library/blocks/post-featured-image/style.css 149 B 0 B
build/block-library/blocks/preformatted/style-rtl.css 119 B 0 B
build/block-library/blocks/preformatted/style.css 119 B 0 B
build/block-library/blocks/pullquote/editor-rtl.css 231 B 0 B
build/block-library/blocks/pullquote/editor.css 231 B 0 B
build/block-library/blocks/pullquote/style-rtl.css 359 B 0 B
build/block-library/blocks/pullquote/style.css 359 B 0 B
build/block-library/blocks/query-loop/editor-rtl.css 142 B 0 B
build/block-library/blocks/query-loop/editor.css 141 B 0 B
build/block-library/blocks/query-loop/style-rtl.css 361 B 0 B
build/block-library/blocks/query-loop/style.css 363 B 0 B
build/block-library/blocks/query/editor-rtl.css 210 B 0 B
build/block-library/blocks/query/editor.css 210 B 0 B
build/block-library/blocks/quote/editor-rtl.css 121 B 0 B
build/block-library/blocks/quote/editor.css 121 B 0 B
build/block-library/blocks/quote/style-rtl.css 215 B 0 B
build/block-library/blocks/quote/style.css 214 B 0 B
build/block-library/blocks/rss/editor-rtl.css 270 B 0 B
build/block-library/blocks/rss/editor.css 270 B 0 B
build/block-library/blocks/rss/style-rtl.css 314 B 0 B
build/block-library/blocks/rss/style.css 313 B 0 B
build/block-library/blocks/search/editor-rtl.css 213 B 0 B
build/block-library/blocks/search/editor.css 213 B 0 B
build/block-library/blocks/search/style-rtl.css 384 B 0 B
build/block-library/blocks/search/style.css 386 B 0 B
build/block-library/blocks/separator/editor-rtl.css 151 B 0 B
build/block-library/blocks/separator/editor.css 151 B 0 B
build/block-library/blocks/separator/style-rtl.css 281 B 0 B
build/block-library/blocks/separator/style.css 281 B 0 B
build/block-library/blocks/shortcode/editor-rtl.css 547 B 0 B
build/block-library/blocks/shortcode/editor.css 547 B 0 B
build/block-library/blocks/site-logo/editor-rtl.css 251 B 0 B
build/block-library/blocks/site-logo/editor.css 251 B 0 B
build/block-library/blocks/site-logo/style-rtl.css 166 B 0 B
build/block-library/blocks/site-logo/style.css 166 B 0 B
build/block-library/blocks/social-link/editor-rtl.css 211 B 0 B
build/block-library/blocks/social-link/editor.css 211 B 0 B
build/block-library/blocks/social-links/editor-rtl.css 749 B 0 B
build/block-library/blocks/social-links/editor.css 749 B 0 B
build/block-library/blocks/social-links/style-rtl.css 1.37 kB 0 B
build/block-library/blocks/social-links/style.css 1.37 kB 0 B
build/block-library/blocks/spacer/editor-rtl.css 321 B 0 B
build/block-library/blocks/spacer/editor.css 321 B 0 B
build/block-library/blocks/spacer/style-rtl.css 107 B 0 B
build/block-library/blocks/spacer/style.css 107 B 0 B
build/block-library/blocks/subhead/editor-rtl.css 148 B 0 B
build/block-library/blocks/subhead/editor.css 148 B 0 B
build/block-library/blocks/subhead/style-rtl.css 134 B 0 B
build/block-library/blocks/subhead/style.css 134 B 0 B
build/block-library/blocks/table/editor-rtl.css 530 B 0 B
build/block-library/blocks/table/editor.css 530 B 0 B
build/block-library/blocks/table/style-rtl.css 433 B 0 B
build/block-library/blocks/table/style.css 433 B 0 B
build/block-library/blocks/tag-cloud/editor-rtl.css 162 B 0 B
build/block-library/blocks/tag-cloud/editor.css 162 B 0 B
build/block-library/blocks/tag-cloud/style-rtl.css 145 B 0 B
build/block-library/blocks/tag-cloud/style.css 145 B 0 B
build/block-library/blocks/template-part/editor-rtl.css 644 B 0 B
build/block-library/blocks/template-part/editor.css 645 B 0 B
build/block-library/blocks/text-columns/editor-rtl.css 146 B 0 B
build/block-library/blocks/text-columns/editor.css 146 B 0 B
build/block-library/blocks/text-columns/style-rtl.css 209 B 0 B
build/block-library/blocks/text-columns/style.css 209 B 0 B
build/block-library/blocks/verse/editor-rtl.css 118 B 0 B
build/block-library/blocks/verse/editor.css 118 B 0 B
build/block-library/blocks/verse/style-rtl.css 137 B 0 B
build/block-library/blocks/verse/style.css 137 B 0 B
build/block-library/blocks/video/editor-rtl.css 547 B 0 B
build/block-library/blocks/video/editor.css 548 B 0 B
build/block-library/blocks/video/style-rtl.css 241 B 0 B
build/block-library/blocks/video/style.css 241 B 0 B
build/block-library/common-rtl.css 940 B 0 B
build/block-library/common.css 937 B 0 B
build/block-library/editor-rtl.css 9.08 kB 0 B
build/block-library/editor.css 9.08 kB 0 B
build/block-library/style-rtl.css 8.48 kB 0 B
build/block-library/style.css 8.48 kB 0 B
build/block-library/theme-rtl.css 789 B 0 B
build/block-library/theme.css 790 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.06 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 171 kB 0 B
build/components/style-rtl.css 15.4 kB 0 B
build/components/style.css 15.4 kB 0 B
build/compose/index.js 11 kB 0 B
build/core-data/index.js 15.4 kB 0 B
build/data-controls/index.js 829 B 0 B
build/data/index.js 8.98 kB 0 B
build/date/index.js 31.8 kB 0 B
build/deprecated/index.js 768 B 0 B
build/dom-ready/index.js 571 B 0 B
build/dom/index.js 4.95 kB 0 B
build/edit-navigation/index.js 11.1 kB 0 B
build/edit-navigation/style-rtl.css 881 B 0 B
build/edit-navigation/style.css 885 B 0 B
build/edit-post/index.js 306 kB 0 B
build/edit-post/style-rtl.css 6.47 kB 0 B
build/edit-post/style.css 6.45 kB 0 B
build/edit-site/index.js 24.4 kB 0 B
build/edit-site/style-rtl.css 3.91 kB 0 B
build/edit-site/style.css 3.91 kB 0 B
build/edit-widgets/index.js 26.3 kB 0 B
build/edit-widgets/style-rtl.css 3.1 kB 0 B
build/edit-widgets/style.css 3.1 kB 0 B
build/editor/editor-styles-rtl.css 476 B 0 B
build/editor/editor-styles.css 478 B 0 B
build/editor/index.js 43.3 kB 0 B
build/editor/style-rtl.css 3.84 kB 0 B
build/editor/style.css 3.84 kB 0 B
build/element/index.js 4.63 kB 0 B
build/escape-html/index.js 735 B 0 B
build/format-library/index.js 6.74 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.27 kB 0 B
build/html-entities/index.js 623 B 0 B
build/i18n/index.js 3.57 kB 0 B
build/is-shallow-equal/index.js 698 B 0 B
build/keyboard-shortcuts/index.js 2.54 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.1 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.32 kB 0 B
build/notices/index.js 1.86 kB 0 B
build/nux/index.js 3.42 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.54 kB 0 B
build/primitives/index.js 1.43 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.84 kB 0 B
build/reusable-blocks/index.js 2.92 kB 0 B
build/rich-text/index.js 13.4 kB 0 B
build/server-side-render/index.js 2.77 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 2.84 kB 0 B
build/viewport/index.js 1.86 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.22 kB 0 B

compressed-size-action

@noahtallen
Copy link
Member

Yeah, we will definitely need to not use pub before merging — that’s just a platform choice, and a theme could be placed under any arbitrary subdirectory. Are we sure that in gutenberg, the theme slug excludes a subdirectory? (You could test by creating a custom mapping from “wp-content/themes/xyz” to a local block based theme in wp-env.)

@@ -23,14 +23,17 @@ export default function useTemplatePartPost( postId, slug, theme ) {
// load the auto-draft created from the
// relevant file.
if ( slug && theme ) {
Copy link
Contributor Author

@jeyip jeyip Dec 15, 2020

Choose a reason for hiding this comment

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

The theme value on line 25 seems misleading. We no longer use it in the template parts query for reasons explained in the PR description (for auto-drafted template parts, it doesn't account for themes that have a parent directory).

At this point, it's really only used to determine if the current template part block is a placeholder or if its auto-drafted. Maybe we should have a follow-up PR to refactor it?

Copy link
Contributor

Choose a reason for hiding this comment

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

A follow up makes sense to me, this does seem a bit outdated with how things work now.

Checking the theme field that was added to the html markup was an early way to resolve things (and as you have noted a bit problematic when theme directories may be nested elsewhere). And now that we are only concerned with auto-drafts from currently activated theme using getCurrentTheme instead seems to make sense.

Copy link
Contributor

@Addison-Stavlo Addison-Stavlo left a comment

Choose a reason for hiding this comment

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

This works great!

Either as an addition to this or follow up we should update:

const currentTheme = select( 'core' ).getCurrentTheme()?.stylesheet;

and
const currentTheme = select( 'core' ).getCurrentTheme()?.stylesheet;

to also properly use that replace( '/', '-' );. - so they properly show up in the nav sidebar and previews as well. 😁

@jeyip
Copy link
Contributor Author

jeyip commented Dec 15, 2020

This works great!

Either as an addition to this or follow up we should update:
gutenberg/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/template-parts.js and to also properly use that replace( '/', '-' );. - so they properly show up in the nav sidebar and previews as well. 😁

That makes total sense 👍 I'll make a quick follow-up after I get this merged.

const cleanedSlug = cleanForSlug( slug );
const themeSlug = currentTheme.replace( '/', '-' );
Copy link
Member

Choose a reason for hiding this comment

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

Will this work for arbitrary paths registered with register_theme_directory?

@jeyip jeyip force-pushed the fix/template-parts-not-loading branch from 81a800f to da9f50e Compare December 16, 2020 00:12
@david-szabo97
Copy link
Member

david-szabo97 commented Dec 16, 2020

It does fix the problem. But I don't think this is the best way to fix this. Also, this change causes another bug where you can't use template parts from a different theme other than your current theme.

After some digging, I realized the theme attribute is wrong in the content of the template files, if we put the theme into a directory.

So to my understanding, the root cause is:

  • Template files are static content
  • If we move the theme into a directory, then the stylesheet changes from twentytwentyone-blocks to my-dir-twentytwentyone-blocks
  • But the content of the template files stay the same
  • Which means, the template parts are still referring to twentytwentyone-blocks instead of my-dir-twentytwentyone-blocks

@david-szabo97
Copy link
Member

For more clarification, here is an example with seedlet-blocks. This is what we have in the block template file:

<!-- wp:template-part {"slug":"header","theme":"seedlet-blocks","align":"full","tagName":"header"} /-->

If we put the theme into a folder called sub then the theme's stylesheet becomes sub-seedlet-blocks. But in the template file we are still referring to seedlet-blocks.

I don't know how we should handle this though, a few ideas:

  1. Leave the theme attribute empty in the template files and handle this special case as empty string = current theme.
  2. Write some magic to automatically replace the theme attribute on theme activate/install or whatever hook
  3. Instead of leaving the theme attribute, use a special value (__CURRENT_THEME__)
  4. Combine 2 and 3, use a special value and replace it automagically on some hook

@Addison-Stavlo
Copy link
Contributor

Addison-Stavlo commented Dec 16, 2020

  1. Leave the theme attribute empty in the template files and handle this special case as empty string = current theme.
  2. Write some magic to automatically replace the theme attribute on theme activate/install or whatever hook
  3. Instead of leaving the theme attribute, use a special value (CURRENT_THEME)
  4. Combine 2 and 3, use a special value and replace it automagically on some hook

I think we are leaning towards option 5: omit the directory path from the theme taxonomy field when we create the CPTs? At that point it would just match the theme attribute in the markup. (#27765)

@jeyip
Copy link
Contributor Author

jeyip commented Dec 16, 2020

I think we are leaning towards option 5: omit the directory path from the theme taxonomy field when we create the CPTs? At that point it would just match the theme attribute in the markup. (#27765)

This makes total sense to me, especially given that, as mentioned by @Copons, if we use the full path and there's a migration where theme directories are switched around, users might “lose” their templates.

Ex. If pub-seedlet-blocks, which had seedlet-blocks living in a parent directory pub, is moved up one level outside of the pub directory, then the theme slug changes from pub-seedlet-blocks to seedlet-blocks. At this point, we'd lose all of our currently existing templates and template parts

@jeyip
Copy link
Contributor Author

jeyip commented Dec 16, 2020

Also, this change causes another bug where you can't use template parts from a different theme other than your current theme.

Side note: @david-szabo97 I had considered this, and I thought I remembered reading somewhere that this was already a self-imposed limitation of full site editing. That, however, might have changed, and either way, stripping any theme directory pathing seems like a better overall solution. Thanks for reviewing 👍

@Addison-Stavlo
Copy link
Contributor

Also, this change causes another bug where you can't use template parts from a different theme other than your current theme.

This would only be true for auto-drafts referenced by a template. In which case assuming current theme might make sense? 🤔

@jeyip jeyip force-pushed the fix/template-parts-not-loading branch from 68c04c5 to 0123a5c Compare December 18, 2020 07:01
Copy link
Contributor

@Addison-Stavlo Addison-Stavlo left a comment

Choose a reason for hiding this comment

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

Wondering about an edge case if we assume current theme for auto-drafts in the template part block:

  • User edits a theme supplied template and makes it a publish / custom version. But never edits the template-part auto-draft it references, so it is still saved by slug/theme and not Id.
  • User then switches themes and uses their custom published template. Now the template part block assumes the auto draft is from the new theme, and the custom template loads an unexpected version of the template part?

'field' => 'slug',
'field' => 'name',
Copy link
Contributor

Choose a reason for hiding this comment

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

Im curious about the switch from slug to name here? It looks like the templates-sync still queries and creates based on slug, so could this potentially cause issues?

Copy link
Contributor Author

@jeyip jeyip Dec 18, 2020

Choose a reason for hiding this comment

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

Im curious about the switch from slug to name here?

When themes are added to taxonomies, a slug is generated that looks like parent-directory-some-theme while the name looks like parent-directory/some-theme

Using name instead of slug allows us to query for the theme on the client without doing any string transformations. We can simply pass through the value of getCurrentTheme().stylesheet (which looks like parent-directory/some-theme, not parent-directory-some-theme), whereas before, we had to do a .replace to remove the / and add a -

Copy link
Contributor Author

@jeyip jeyip Dec 18, 2020

Choose a reason for hiding this comment

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

It looks like the templates-sync still queries and creates based on slug, so could this potentially cause issues?

Ahhh I must've missed a few values. I'll take a closer look at the php files to find other instances. But yeah. Ideally all queries with slug are switched to name if we go down this route.

I know that, in previous discussions, one of our concerns was other developers having to "know" implicit rules about how to query template parts by theme. For example, as mentioned above, they might have to be aware that, when they query, they need to use a .replace method on getCurrentTheme().stylesheet. With this change, it's no longer the case.

I'd love your thoughts though

Copy link
Contributor

Choose a reason for hiding this comment

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

That all makes sense. Im not really sure if there are any other implications to using name over slug. But if we are consistent with it and it works, it sounds great to have the tax term and getCurrentTheme().stylesheet in sync.

@jeyip
Copy link
Contributor Author

jeyip commented Dec 18, 2020

Wondering about an edge case if we assume current theme for auto-drafts in the template part block:

  • User edits a theme supplied template and makes it a publish / custom version. But never edits the template-part auto-draft it references, so it is still saved by slug/theme and not Id.
  • User switches themes and uses their custom published template. Now the template part block assumes the auto draft is from the new theme, and the custom template loads an unexpected version of the template part?

Good questions! I'm going to think these over

@jeyip
Copy link
Contributor Author

jeyip commented Dec 18, 2020

User edits a theme supplied template and makes it a publish / custom version. But never edits the template-part auto-draft it references, so it is still saved by slug/theme and not Id.

User switches themes and uses their custom published template. Now the template part block assumes the auto draft is from the new theme, and the custom template loads an unexpected version of the template part?

Very keen observation. The most natural way to handle that scenario seems like what we already use, which is theme block attributes 🤔

There's definitely a path forward, but in that case, the solution here would depend on other changes as well:

  • We'd need to remove theme block attributes from theme provided template parts, because they're static values that don't account for theme directory (the theme value is always some-theme, when really we want to query for some-directory/some-theme) I know that the themes team has already expressed desire for this.
  • We could then check if a template part doesn't have a theme block attribute assigned when loading the site editor, and subsequently assign it with getCurrentTheme().stylesheet.

@Addison-Stavlo
Copy link
Contributor

We'd need to remove theme block attributes from theme provided template parts, because they're static values that don't account for theme directory (the theme value is always some-theme, when really we want to query for some-directory/some-theme) I know that the themes team has already expressed desire for this.
We could then check if a template part doesn't have a theme block attribute assigned when loading the site editor, and subsequently assign it with getCurrentTheme().stylesheet.

🤔 Even with that I still see the same scenario happening. Customizing a template from one theme without customizing the template part, switching to another theme, using that first customized template... the template part will still correspond to the new theme instead of the one the template originally referenced. Its an edge case but Im not sure how concerning it should be.

@vindl vindl requested a review from youknowriad December 18, 2020 20:04
@jeyip
Copy link
Contributor Author

jeyip commented Dec 18, 2020

Even with that I still see the same scenario happening. Customizing a template from one theme without customizing the template part, switching to another theme, using that first customized template...

  • I should clarify that to account for that scenario, we'd also remove this and this change from this PR.
  • We would stop querying auto drafts by the current theme and return to querying by theme block attributes instead.
  • I believe that, by no longer querying for the current theme's template parts, and by implementing the changes I described earlier, it would query the previous theme's auto-drafted template parts, not the new ones, in a theme switch.

@Addison-Stavlo
Copy link
Contributor

Addison-Stavlo commented Dec 18, 2020

by implementing the changes I described earlier, it would query the previous theme's auto-drafted template parts

Oh, I think I get what you were saying earlier then.

if a template part doesn't have a theme block attribute assigned when loading the site editor, and subsequently assign it with getCurrentTheme().stylesheet.

Im catching on, 😆 - so we would assign that theme attribute so it would be the appropriate value considering the directories. The problem with this approach means whenever we load the editor with a new theme, the editor will initially load in the dirty state since we have changed attributes of the template part block. We previously had some behavior that caused this sort of thing and it was definitely unfavorable. 🤔 To avoid this we would have to parse and change the markup at the time we are creating the auto-draft of the template itself to assign that correct theme attribute on the template part blocks appropriately. Im not sure if thats the right way to go either.

@jeyip
Copy link
Contributor Author

jeyip commented Dec 18, 2020

We previously had some behavior that caused this sort of thing and it was definitely unfavorable. 🤔 To avoid this we would have to parse and change the markup at the time we are creating the auto-draft of the template itself to assign that correct theme attribute on the template part blocks appropriately. Im not sure if thats the right way to go either.

Oh yikes. Thanks for the context 👍 I wasn't aware that this was an issue. In that case, I wonder if we can move forward with this PR at all without tradeoffs. 🤔

@jeyip jeyip closed this Jan 12, 2021
@jeyip
Copy link
Contributor Author

jeyip commented Jan 12, 2021

Now being handled by other PRs like #28088

@Copons Copons deleted the fix/template-parts-not-loading branch January 12, 2021 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants