Skip to content

Commit

Permalink
Inject theme stylesheet value as template part theme attribute (#53423)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Norris <sarah@sekai.co.uk>
  • Loading branch information
pbking and mikachan authored Aug 15, 2023
1 parent 2bb2d55 commit ed6d406
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
store as blockEditorStore,
useBlockProps,
} from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';

const PatternEdit = ( { attributes, clientId } ) => {
const selectedPattern = useSelect(
Expand All @@ -18,12 +19,42 @@ const PatternEdit = ( { attributes, clientId } ) => {
[ attributes.slug ]
);

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

const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
const { setBlockEditingMode } = useDispatch( blockEditorStore );
const { getBlockRootClientId, getBlockEditingMode } =
useSelect( blockEditorStore );

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;
}

// Run this effect when the component loads.
// This adds the Pattern's contents to the post.
// This change won't be saved.
Expand All @@ -40,7 +71,9 @@ const PatternEdit = ( { attributes, clientId } ) => {
// Clone blocks from the pattern before insertion to ensure they receive
// distinct client ids. See https://github.com/WordPress/gutenberg/issues/50628.
const clonedBlocks = selectedPattern.blocks.map( ( block ) =>
cloneBlock( block )
cloneBlock(
injectThemeAttributeInBlockTemplateContent( block )
)
);
const rootEditingMode = getBlockEditingMode( rootClientId );
// Temporarily set the root block to default mode to allow replacing the pattern.
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/pattern/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function render_block_core_pattern( $attributes ) {
}

$pattern = $registry->get_registered( $slug );
$content = $pattern['content'];
$content = _inject_theme_attribute_in_block_template_content( $pattern['content'] );

$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && ! empty( $gutenberg_experiments['gutenberg-auto-inserting-blocks'] ) ) {
Expand Down

0 comments on commit ed6d406

Please sign in to comment.