WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.4k stars 4.16k forks source link

Patterns: allow overriding of theme patterns as well as duplication #55911

Open glendaviesnz opened 11 months ago

glendaviesnz commented 11 months ago

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
talldan commented 11 months ago

There's a similar conversation happening on this issue - https://github.com/WordPress/gutenberg/issues/55469

glendaviesnz commented 11 months ago

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 commented 10 months ago

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

talldan commented 5 months ago

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 - https://github.com/WordPress/gutenberg/issues/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:

(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 commented 5 months ago

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 commented 3 months ago

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 commented 3 months ago

@MadtownLems That a good thing to call out. There's this related (older) issue - https://github.com/WordPress/gutenberg/issues/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).