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

Block theme: Dynamic pattern includes broken with GB 18.1 #662

Closed
ryelle opened this issue Apr 12, 2024 · 1 comment · Fixed by #686
Closed

Block theme: Dynamic pattern includes broken with GB 18.1 #662

ryelle opened this issue Apr 12, 2024 · 1 comment · Fixed by #686
Assignees
Labels
[Component] Core/Gutenberg Changes that should happen in the core editor (tracking issues only) [Component] Theme The frontend of the pattern directory, pattern lists UI

Comments

@ryelle
Copy link
Contributor

ryelle commented Apr 12, 2024

In the new Pattern Directory theme, there is code to swap out a pattern based on the current page context. For example, viewing a single pattern uses the single.html template, which contains <!-- wp:pattern {"slug":"wporg-pattern-directory-2024/single-pattern"} /-->. We use patterns like template parts so that we can use PHP i18n functions.

In a render_block_data hook, there's a check for pattern ownership, and if the current user owns the pattern, we swap out the slug to show the wporg-pattern-directory-2024/single-my-pattern pattern. This is also done for showing logged out messaging on the "my patterns" and "favorites" pages.

/**
* Update header template based on current query.
*
* @param array $parsed_block The block being rendered.
*
* @return array The updated block.
*/
function modify_pattern_include( $parsed_block ) {
if ( 'core/pattern' !== $parsed_block['blockName'] || empty( $parsed_block['attrs']['slug'] ) ) {
return $parsed_block;
}
if (
'wporg-pattern-directory-2024/single-pattern' === $parsed_block['attrs']['slug'] &&
get_current_user_id() === get_the_author_meta( 'ID' )
) {
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/single-my-pattern';
}
if (
'wporg-pattern-directory-2024/grid-mine' === $parsed_block['attrs']['slug'] &&
! get_current_user_id()
) {
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/logged-out-patterns';
}
if (
'wporg-pattern-directory-2024/grid-favorites' === $parsed_block['attrs']['slug'] &&
! get_current_user_id()
) {
$parsed_block['attrs']['slug'] = 'wporg-pattern-directory-2024/logged-out-favorites';
}
return $parsed_block;
}

What's the problem?

In Gutenberg 18.1, some code was added to replace the patterns earlier WordPress/gutenberg#60349. This seems to not run render_block_data anymore, so the pattern conditional never kicks in. This means users don't see the correct actions on a pattern they own.

Currently wp.org is pinned to 17.8.1 (though I'm going to try updating it to 18.0 on monday), so this won't affect us immediately. GB was unpinned, so this does impact us now.

More details

We do something similar in the Developer theme, but with the template-part block, so that should continue to work.

An alternate solution could be to add a new template into the hierarchy for logged in, but the current pattern-swap approach lets us update discrete content, rather than duplicating entire templates.

@ryelle ryelle added [Component] Theme The frontend of the pattern directory, pattern lists UI [Component] Core/Gutenberg Changes that should happen in the core editor (tracking issues only) labels Apr 12, 2024
@ryelle
Copy link
Contributor Author

ryelle commented Apr 17, 2024

I've updated w.org to pin Gutenberg to 18.0.1 while we figure out if this will be fixed in Gutenberg or if we need a new include workaround.

@ryelle ryelle self-assigned this May 2, 2024
ryelle added a commit that referenced this issue May 2, 2024
Fixes #662 — This removes the pattern slug filtering in favor of adding extra custom templates, and filtering to inject those into the hierarchy. This is a more core-native way to handle template overrides, so it will be less likely to break in the future.
ryelle added a commit that referenced this issue May 6, 2024
* Block theme: Update pattern workaround to use template filters.

Fixes #662 — This removes the pattern slug filtering in favor of adding extra custom templates, and filtering to inject those into the hierarchy. This is a more core-native way to handle template overrides, so it will be less likely to break in the future.

* Fix the content alignment of logged out messages

* Tools: Update packages, unpin Gutenberg from composer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Component] Core/Gutenberg Changes that should happen in the core editor (tracking issues only) [Component] Theme The frontend of the pattern directory, pattern lists UI
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant