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: allow overriding of theme patterns as well as duplication #55911

Open
glendaviesnz opened this issue Nov 7, 2023 · 7 comments
Open
Labels
[Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced [Type] Enhancement A suggestion for improvement.

Comments

@glendaviesnz
Copy link
Contributor

glendaviesnz commented Nov 7, 2023

What problem does this address?

Currently, it is only possible to duplicate a theme pattern in the site editor pattern library. This creates a completely new pattern that lives in the pattern library in conjunction with the original theme pattern, and changes to it do not impact the places where the original theme pattern is used.

What is your proposed solution?

It would be useful to have another option that replaces the theme patterns with the new custom copy, and changes to this pattern are reflected everywhere that the theme pattern is used in the site. It should also then be possible to revert the pattern back to the original theme version. This would be similar to how template modifications currently work, with any edits to a theme template overriding the original theme template, but with an option to revert the customisations:

Screenshot 2023-11-07 at 2 18 33 PM
@glendaviesnz glendaviesnz added [Type] Enhancement A suggestion for improvement. [Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced labels Nov 7, 2023
@glendaviesnz glendaviesnz changed the title Patterns: allow overriding of theme patters as well as duplication Patterns: allow overriding of theme patterns as well as duplication Nov 7, 2023
@glendaviesnz glendaviesnz moved this to Todo in Patterns Nov 7, 2023
@talldan
Copy link
Contributor

talldan commented Nov 7, 2023

There's a similar conversation happening on this issue - #55469

@glendaviesnz
Copy link
Contributor Author

There's a similar conversation happening on this issue - #55469

It is a little broader in relation to the lock icon, etc. so I think it is worth keeping this issue for the specific functionality of overriding rather than duplicating, but feel free to close if you don't think we need both.

@richtabor
Copy link
Member

Yes, I'd expect these to work exactly like template parts.

@talldan
Copy link
Contributor

talldan commented Apr 19, 2024

I think this has been discussed in other places, but I have some thoughts on the technical solution for this.

The first steps would be to make the wp_block post type support slugs, and have the pattern (wp:block block) reference those post types via the slug (instead of the id).

When you customize a bundled pattern, a wp_block post type would use the same slug, and that's how we'd tie the two things together. It's the exact same way that template parts work, so it helps with the unification effort - #57011. It also means bundled theme patterns can be 'synced' from the start as described in #59272, the same way template parts are.

@getdave has already been exploring a similar change for wp_navigation post types:

So it'd be good to try getting that merged first and then explore using the same approach for patterns. The gist of it is:

  • Pattern Block: A new slug attribute would be introduced, with the old ref attribute soft deprecated (it would still work for back compat, but any new usage uses the slug).
  • REST API: No/minimal changes, a query is used to fetch by either slug or id depending on the reference the block has (e.g. wp/v2/wp_block?slug=twentytwentyfour/header.
  • Core data: Would need some adaptation to know when to use the right approach for getEntityRecord dependent on slug or id being used.
  • Client side permissions checks: There's a tricky issue that permissions checks for individual records wouldn't be able to work the same way. canUser( 'edit', 'wp_block', 'twentytwentyfour/footer' ) wouldn't work out of the box, as it translates to OPTIONS /wp/v2/wp_block/twentytwentyfour/footer which the REST API doesn't know how to handle.
  • Migrating old wp_block posts: Avoid a one-time migration, instead add slugs to records when the user saves and prefer slugs for any new records.

(In the future it might be an option to move to a /wp/v2/patterns endpoint that returns bundled patterns, template parts and wp_blocks all in one go and uses slugs as part of the unification work. I'm not sure if that's something that's worth bringing forwards for this)

@getdave
Copy link
Contributor

getdave commented Apr 22, 2024

Core data: Would need some adaptation to know when to use the right approach for getEntityRecord dependent on slug or id being used.

Just a note to say that in exploring this for Navigation I did consider extending the Posts Controller for Navigation and adding a slug endpoint. It worked well but required overloading a few methods.

However the feedback from REST maintainers was that we shouldn't do this just for Navigation and instead we should utilise the existing collections endpoints to fetch by slug and get the 0th record.

However, if we have other uses cases perhaps we could explore being able to fetch other posts by slug.

@MadtownLems
Copy link

MadtownLems commented Jun 18, 2024

I think allowing customizations to theme-provided Patterns has potential for some sites, but can imagine plenty of environments (such as ours) where we wouldn't want to allow it. Hopefully that's the kind of thing that could be disabled via code. 🤞

Duplication sounds great, though!

@talldan
Copy link
Contributor

talldan commented Jun 19, 2024

@MadtownLems That a good thing to call out. There's this related (older) issue - #28895.

I also seem to recall a related conversation around template parts here on github, though I can't remember specifics now.

#28895 has been added to the Synced Patterns iteration for WordPress 6.7 (Draft).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced [Type] Enhancement A suggestion for improvement.
Projects
No open projects
Status: Todo
Development

No branches or pull requests

5 participants