WordPress / gutenberg

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

Add the ability to lock/make exclusive patterns to template parts #47208

Open jonathan-dejong opened 1 year ago

jonathan-dejong commented 1 year ago

What problem does this address?

Presently it does not seem to be possible to create patterns that should be unique to a specific template part. Say I'm building a hybrid theme where I am only using template parts but not templates. I have a template part for my footer and I want to present the user with two variations of that footer. I could create two patterns that represents all the blocks and layout of each variation.

However those patterns does not make sense to expose outside the editing of this template part, they just become clutter for the user.

What is your proposed solution?

The ability to actually lock down a pattern to only appear in the context of to which part of Gutenberg it has been assigned. We already have template locking for templates and individual blocks so to me it makes sense to utilise the same standard on this. So similarly to how you can set blockTypes on registering a pattern you could also set templateLock.

register_block_pattern(
    'wpdocs-my-plugin/my-awesome-pattern',
    array(
        'title'        => __( 'Two buttons', 'wpdocs-my-plugin' ),
        'description'  => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
        'content'      => '…',
        'templateLock' => array( 'core/template-part/footer' ),
    )
);
annezazu commented 1 year ago

This feels related to https://github.com/WordPress/gutenberg/pull/45814 and https://github.com/WordPress/gutenberg/pull/45285 cc @jorgefilipecosta as a heads up!

annezazu commented 1 year ago

To understand a bit better, is there a reason you wouldn't want to create two footer template parts that a user could pick between?

jonathan-dejong commented 1 year ago

Please note that I am talking about a so called "hybrid" theme in this issue. So a theme that does not have a templates/ directory and the index.html file.

So if I am to allow the selection of separate template parts for the same section I would need to come up with my own way of letting the admin pick which template to render in a specific location. Because I would be using the block_template_part() function in a theme php template file. https://developer.wordpress.org/reference/functions/block_template_part/

If I was developing a full FSE theme I think you are right, I could probably just have two different footer template parts and then let the admin choose which one they want to assign to the footer template 🤔

But in this case I need to work with a single template part, but have restricted options for what to add into that area.

Does that make more sense?

annezazu commented 1 year ago

But in this case I need to work with a single template part, but have restricted options for what to add into that area.

Yes! Totally makes sense. This should be improved when we add this option too: https://github.com/WordPress/gutenberg/issues/44582

jonathan-dejong commented 1 year ago

Yes that looks interesting!