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

Patterns: Inject the theme name into the block attributes #54595

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ function useStartPatterns( fallbackContent ) {
};
}, [] );

const currentThemeStylesheet = useSelect(
( select ) => select( coreStore ).getCurrentTheme().stylesheet
);

// Duplicated from packages/block-library/src/pattern/edit.js.
Copy link
Contributor

Choose a reason for hiding this comment

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

should we make a similar comment in that file so any future changes get ported to this function too?

function injectThemeAttributeInBlockTemplateContent( block ) {
if (
block.innerBlocks.find(
( innerBlock ) => innerBlock.name === 'core/template-part'
)
) {
block.innerBlocks = block.innerBlocks.map( ( innerBlock ) => {
if (
innerBlock.name === 'core/template-part' &&
innerBlock.attributes.theme === undefined
) {
innerBlock.attributes.theme = currentThemeStylesheet;
}
return innerBlock;
} );
}

if (
block.name === 'core/template-part' &&
block.attributes.theme === undefined
) {
block.attributes.theme = currentThemeStylesheet;
}
return block;
}

return useMemo( () => {
// filter patterns that are supposed to be used in the current template being edited.
return [
Expand All @@ -68,7 +99,12 @@ function useStartPatterns( fallbackContent ) {
);
} )
.map( ( pattern ) => {
return { ...pattern, blocks: parse( pattern.content ) };
return {
...pattern,
blocks: parse( pattern.content ).map( ( block ) =>
injectThemeAttributeInBlockTemplateContent( block )
),
};
} ),
];
}, [ fallbackContent, slug, patterns ] );
Expand Down
Loading