Skip to content

Conversation

@ramonjd
Copy link
Member

@ramonjd ramonjd commented Oct 14, 2025

Builds upon #10180

Props to @scruffian

Modifies the resolve_pattern_blocks function to include metadata for single-root patterns, allowing the pattern name and title to be stored in the block attributes. The metadata contains the patternName and name attributes.

This enables identification of patterns inside templates and allows for special treatment in the editor.

This will be used to test contentOnly editing (WordPress/gutenberg#71517), so that themes that use patterns automatically opt into this behaviour.

Trac ticket: https://core.trac.wordpress.org/ticket/64123


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Oct 14, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly, andrewserong, talldanwp, westonruter, scruffian.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ramonjd ramonjd force-pushed the try/add-metadata-to-parsed-pattern-root-block-attributes branch 3 times, most recently from b5812e5 to d8f1d20 Compare October 21, 2025 01:01
@ramonjd ramonjd changed the title Add the pattern name to pattern blocks when they are converted [unit tests] Patterns: Add the pattern name to pattern blocks when they are converted Oct 21, 2025
@ramonjd ramonjd self-assigned this Oct 21, 2025
@scruffian
Copy link
Contributor

Is there other metadata we could/should add to patterns - some of them seem to also contain a longer description. It would be good to support as much metadata as possible IMO.

@ramonjd
Copy link
Member Author

ramonjd commented Oct 22, 2025

Is there other metadata we could/should add to patterns - some of them seem to also contain a longer description. It would be good to support as much metadata as possible IMO.

Good question. What do you think the editor might need? There are also optional categories and block types, which could come in handy if the editor ever wanted to extend "change design" to patterns bundled with templates.

I'm not sure.

@ramonjd ramonjd requested review from scruffian and talldan October 22, 2025 22:20
@scruffian
Copy link
Contributor

The description would be useful for WordPress/gutenberg#72574. Not sure about the others.

@ramonjd ramonjd force-pushed the try/add-metadata-to-parsed-pattern-root-block-attributes branch from d8f1d20 to ed8d449 Compare October 24, 2025 01:45
@ramonjd
Copy link
Member Author

ramonjd commented Oct 24, 2025

The description would be useful for WordPress/gutenberg#72574.

Done!

@ramonjd ramonjd force-pushed the try/add-metadata-to-parsed-pattern-root-block-attributes branch 2 times, most recently from ef3a7bc to a640eaa Compare November 10, 2025 05:13
Comment on lines 1884 to 1886
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );

$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these lines be duplicated like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

WUT? No they shouldn't 😄

Thanks for spotting that. Human paste error.

Comment on lines 1884 to 1886
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );

$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );

Comment on lines 1889 to 1891
* For single-root patterns, add the pattern name to make this a pattern instance in the editor.
* If the pattern has metadata, merge it with the existing metadata.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* For single-root patterns, add the pattern name to make this a pattern instance in the editor.
* If the pattern has metadata, merge it with the existing metadata.
*/
* For single-root patterns, add the pattern name to make this a pattern instance in the editor.
* If the pattern has metadata, merge it with the existing metadata.
*/

Comment on lines 1897 to 1902
* Merge pattern metadata with existing block metadata.
* Pattern metadata takes precedence, but existing block metadata
* is preserved as a fallback when the pattern doesn't define that field.
* Only the defined fields (name, description, categories) are updated;
* other metadata keys are preserved.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Merge pattern metadata with existing block metadata.
* Pattern metadata takes precedence, but existing block metadata
* is preserved as a fallback when the pattern doesn't define that field.
* Only the defined fields (name, description, categories) are updated;
* other metadata keys are preserved.
*/
* Merge pattern metadata with existing block metadata.
* Pattern metadata takes precedence, but existing block metadata
* is preserved as a fallback when the pattern doesn't define that field.
* Only the defined fields (name, description, categories) are updated;
* other metadata keys are preserved.
*/

) as $key => $pattern_key ) {
$value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null;
if ( $value ) {
$block_metadata[ $key ] = 'categories' === $key && is_array( $value )
Copy link
Member

Choose a reason for hiding this comment

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

Is the $key check needed here? Shouldn't array_map() always be used when the value is an array, regardless of the key?

Suggested change
$block_metadata[ $key ] = 'categories' === $key && is_array( $value )
$block_metadata[ $key ] = is_array( $value )

Copy link
Member Author

Choose a reason for hiding this comment

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

By jove, you're right! I just commit stuff when it works

…r single-root patterns, allowing the pattern name and title to be stored in the block attributes.

Adds unit tests.
…cks` function

Enhances the `resolve_pattern_blocks` function to include additional metadata attributes such as `description` and `categories` for single-root patterns. Updates unit tests to validate the new functionality, including sanitization of potentially harmful characters in pattern attributes.
Refines the logic for merging metadata in single-root patterns, ensuring that existing metadata is preserved and new attributes such as `name`, `description`, and `categories` are correctly integrated. Updates unit tests to validate the new behavior, including a test for preserving existing metadata.
…teardown in `resolvePatternBlocks` test suite.
Improves the handling of metadata for single-root patterns by ensuring that existing metadata is preserved while integrating new attributes such as `name`, `description`, and `categories`. Updates unit tests to reflect the changes in metadata structure, ensuring accurate representation of merged metadata.
…n single-pattern container blocks for version 7.0.0.
Introduces a new block pattern 'core/with-custom-metadata' with custom metadata keys. Updates the unit tests to ensure that custom metadata is preserved when resolving patterns, enhancing the test coverage for metadata handling in block patterns.
Improves the documentation within the `resolve_pattern_blocks` function by enhancing comments related to metadata handling for single-root patterns. This change aims to provide clearer guidance on the merging process of pattern metadata with existing block metadata.
@ramonjd ramonjd force-pushed the try/add-metadata-to-parsed-pattern-root-block-attributes branch from a640eaa to 44c4c8f Compare November 26, 2025 04:46
ramonjd added a commit to WordPress/gutenberg that referenced this pull request Nov 26, 2025
ramonjd added a commit to WordPress/gutenberg that referenced this pull request Nov 26, 2025
peterwilsoncc pushed a commit to peterwilsoncc/gutenberg-build that referenced this pull request Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants