WordPress / gutenberg

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

Site Editing: Plugin conditional templates #29076

Open Sandstromer opened 3 years ago

Sandstromer commented 3 years ago

What problem does this address?

Should a theme author wish to include a template that includes blocks(s) from another plugin, the template is available to edit/use without the dependent plugin being active.

Examples could be a product template designed to work with WooCommerce blocks, or a header template containing the WC product search.

There could be confusion should a user select the header template for use on a site where WooCommerce is not in use.

What is your proposed solution?

Have some way of a theme defining when a particular template/template part is available for use in the editor.

Could this be included in theme.json?

e.g.

{
  "pluginTemplates": [
    {
      "slug": "shop-header",
      "plugin": "woocommerce"
    },
    {
      "slug": "shop-sidebar",
      "plugin": "woocommerce"
    }
  ]
}

Run a check to see if the plugin in the woocommerce directory is active. If so, make the shop-header template available in the editor.

talldan commented 3 years ago

@Sandstromer What happens right now? Do the blocks show as missing?

Sandstromer commented 3 years ago

What happens right now? Do the blocks show as missing?

It very much depends on the block.

For example, some blocks will display - on the frontend - the html parts of the block but not the dynamic content. While in the editor the same block may display a warning message such as Your site doesn’t include support for the "woocommerce/featured-category" block. You can leave this block intact, convert its content to a Custom HTML block, or remove it entirely.

With block patterns we can conditionally make them available because we use php.

I'm not sure whether we need conditional templates, or whether we can make do with block patterns?

Would love to hear some more thoughts on this.

markhowellsmead commented 2 years ago

Is this the correct ticket in which to mention conditional block display? For example, I need to be able to switch between a query loop and a "no posts found" message in e.g. block-templates/index.html. Is this not possible, or have I just not found the documentation yet? Without this, we're blocked from using the FSE features for all of our sites.

webmandesign commented 1 year ago

I'd like to point out that this should be applicable not only for templates but also template parts. (As described in #51987)

In my opinion, the most flexible solution would be to provide a conditionCallback attribute for templates and template parts defined in theme.json. Such as:

"templateParts": [
    {
        "title": "Product archive content",
        "name": "woocommerce-archive-product",
        "conditionCallback": "is_woocommerce_active"
    }
],

"templates": [
    {
        "title": "Product archive",
        "name": "archive-product",
        "conditionCallback": "is_woocommerce_active"
    }
]

The conditionCallback value could be a PHP function name to call when checking whether the template or template part should be visible/accessible to a user.

I'm aware there is no "templates" key in theme.json, but maybe it would be beneficial to introduce such key so we can control templates this way. The "customTemplates" key does not really help here as those templates should not be recognized as custom templates by WordPress either. They should not be available to choose from on page edit screen (as can be seen in #51987):

wc-templates-custom