WordPress / gutenberg

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

Synced Patterns (Reusable Blocks) error in "My Patterns" screen, but not in Post Type Editor #53442

Open kimcoleman opened 1 year ago

kimcoleman commented 1 year ago

Description

In LifterLMS, we use reusable blocks (post type = wp_block) to allow admins to build reusable registration and profile form sections.

These can be edited within the LifterLMS > Forms admin screen, as well as via the admin wp_block post type list and editor. This would be for URLs like the following:

/wp-admin/edit.php?post_type=wp_block No error for the LifterLMS reusable blocks.

/wp-admin/post.php?post=5&action=edit No error for the LifterLMS reusable blocks.

In the My Patterns screen (in WordPress 6.3), though, all of these reusable blocks (synced patterns) are returning an error. "Your site does not include support for the "llms/form-field-confirm-group" block.".

This is only in the editor.

We'd like a filter to exclude our patterns from displaying in this interface. Our developers have not found one available yet. We believe this is traced down to the fact that we limit the post types our reusable blocks can be inserted into. So a filter to exclude patterns there would be ideal.

Step-by-step reproduction instructions

  1. Install LifterLMS
  2. The plugin creates a few primary wp_block post types on activation
  3. Navigate to the My Patterns screen.
  4. All the LifterLMS wp_block post types will show an error.

Screenshots, screen recording, code snippet

Screenshot 2023-08-07 at 9 52 30 PM

The post editor / block editor in the WP admin successfully allowing you to edit the synced pattern for LifterLMS form group.

Screenshot 2023-08-07 at 9 52 22 PM

The My Patterns screen in Site Editor that does not allow you to edit or recognize the synced pattern for LifterLMS form group.

Environment info

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

kimcoleman commented 1 year ago

Adding context here from our developer.

The problem seems to be rooted in the fact that the patterns use some custom blocks that are only registered for certain post types (llms-form and wp_block)

We need a way to register the blocks to be used within the context of the wp_block post type and still appear in the Site Editor / My Patterns context ( which isn't in the context of editing a wp_block post type ).

glendaviesnz commented 1 year ago

Are you able to provide any details about how you currently limit the registration of the blocks to specific post types?

eri-trabiccolo commented 1 year ago

Hey @glendaviesnz here's how: https://github.com/gocodebox/lifterlms-blocks/blob/trunk/src/js/blocks/index.js#L159-L161

Thanks!

glendaviesnz commented 1 year ago

Thanks for the extra detail @eri-trabiccolo, a couple of suggestions:

  1. You could add an additional check to also register your blocks when in the site editor context. Currently there is a global js variable pagenow registered which will be set to site-editor when in the site editor context. Another option is to check for the presence of the site editor selectors.
  2. Alternatively you could register the blocks by default and then use context in the blocks edit methods to decide if you should render them in particular editor contexts. If you set the blocks to usesContext[postId, postType] in their block.json and then in the edit method do something like:
    export default function MyBlock( {
    context: { postType, postId },
    } ) {
    if ( postId && ! [ 'llms_form', 'wp_block' ].includes( postType )) {
        return '<div>You are not able to use this block in this context</div>';
    }
    }

    postType and postId will both be undefined in the context of the site editor patterns management page.

Get back to us if neither of those work for you and we can explore other options.

eri-trabiccolo commented 1 year ago

Hey @glendaviesnz many thanks. We considered to register the blocks by default and then use context in the blocks edit methods to whether or not displaying the specific block, but we would prefer to not clutter the block lists that much. I'm gonna try your solutions and decide what suits the best, because we actually need to register/edit some blocks from within some site templates as well (templates for the custom post types...). Thanks again! <3