-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add: Post type restriction API for patterns. #41842
Conversation
Size Change: +58 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
() => unionBy( settingsBlockPatterns, restBlockPatterns, 'name' ), | ||
[ settingsBlockPatterns, restBlockPatterns ] | ||
); | ||
const blockPatterns = useMemo( () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is the right place to do this(editor settings). I haven't tested this but an example could be in a page
insert a Query Loop
that shows let's say book
post type. Shouldn't we show the related patterns there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ntsekouras,
The idea of the postType property in a pattern is to restrict that pattern to be used only in a given post type, and excluded from all the other post types.
So, for example, a pattern that is an arrangement of how a single book can be rendered e.g: with image at the site, description, etc could be restricted to the book post type. This way the pattern will only be available in books.
A pattern that is a query of books and that the plan is to make available to insert on pages or templates etc should not use post-type books as that pattern is not restricted to book post types.
@jorgefilipecosta and @ntsekouras any update on this PR? I do not believe the documentation on |
Hi @ndiego, This PR seems ready and just needs approval. |
42f9062
to
b191543
Compare
b191543
to
f00b084
Compare
Could be tested with the following sample code:
|
@jorgefilipecosta I just tested this, and it works great, but it does make the patterns unavailable in the Site Editor. I'm assuming that is the intended result, but perhaps something we add to the documentation of this property? |
In thinking more about this, I think they should be available in the Site Editor. Perhaps we add another property to enable/disable patterns in the Site Editor 🤔 |
Hi @ndiego, I think yes we should document, but it is expected they are not part of the site editor. The patterns using this are supposed to be used as content of specific post types. The site editor is editing templates and not the content.. |
Yeah, that makes sense. All looks good to me then. LGTM! |
Sorry for repeating my question, but why not make this restriction via REST API by introducing the |
Hi @Mamaduka, here when getting patterns for a post, we also want patterns that don't specify any post type, not just patterns with post type. Normally in rest API filter parameters, if we specify a parameter, we return objects matching it e.g: post type post would return only patterns with postType post and not the patterns that don't contain post type. So this rest API endpoint parameter would work a little bit differently. Also, this feature is something we are experimenting with so I would prefer to avoid that change to the REST API and see where the feature goes and in the future, we can move the filtering to the rest API and do the optimization. |
Thanks for the clarification, @jorgefilipecosta. 🙇 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as advertised.
Regarding the site editor & the patterns not being available there if they have a postType declared. Note that a pattern can declare that it should also be listed for For example, this pattern will only be listed in the site editor: // Only listed at the site editor
register_block_pattern( 'custom-template-pattern', array(
'title' => _x( 'A template pattern', 'Template pattern ', 'gutenberg' ),
'postTypes' => array( 'wp_template' ),
'content' => '<p>Custom Template Pattern</p>'
) ); |
Nice to see this merged! Should it be tagged for 6.1 porting? |
@mcsf Yeah, Matias just covered this in the 6.1 walkthrough, so it would be ported in. |
This PR implements the postTypes restriction for patterns. If a pattern specifies some postTypes the pattern will only be show if one of the post types is being edited.
API
Depends on #41791 which changes the rest endpoint to include the post-type property.