Skip to content

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

Closed

Description

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

[Component] Core/GutenbergChanges that should happen in the core editor (tracking issues only)[Component] ThemeThe frontend of the pattern directory, pattern lists UI

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions