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
Blocks: Add a way to manage block categories #7606
Conversation
73b600b
to
979c13a
Compare
979c13a
to
5dd18a5
Compare
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.
LGTM!
It is possible to filter the list of default block categories using the `block_categories` filter. You can do it on the server by implementing a function which returns a list of categories. It is going to be used during blocks registration and to group blocks in the inserter. You can also use the second provided param `$post` to generate a different list depending on the post's content. | ||
|
||
```php | ||
add_filter( 'block_categories', function( $categories, $post ) { |
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.
FYI this example is not PHP 5.2.x compliant (with use of closure, vs. named function).
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.
Yes, phpcs
complained when I pasted it into codebase. I will split it into 5.2.x style before merging.
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.
Done in e75c4f3.
@@ -80,10 +80,8 @@ export const fallbackBlockName = createBlockNameSetterReducer( 'SET_FALLBACK_BLO | |||
* @return {Object} Updated state. | |||
*/ | |||
export function categories( state = DEFAULT_CATEGORIES, action ) { |
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.
Should we want to keep the DEFAULT_CATEGORIES
in the client?
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.
That question will come up with the blocks as well. I think we need a way to generate scripts bundled as separate modules in case someone wants to use Gutenberg fully client-side.
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 think it seems fine to have a default set in the client. From purely a maintainability perspective, it would be nice if we could avoid maintaining two separate sets of the same values though (e.g. somehow inject into the client JS from the server default value, or vice-versa).
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.
Yes, I thought about that, too. It would be nice to have it in one place, but it is tricky because of translations. I opted for duplication to move on. I open a follow-up issue where we can discuss how to tackle the standalone case.
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.
There's already some previous work done by @aduth bin/get-server-blocks.php
that can achieve something close to what we need.
Description
Supersedes #1732.
With this PR, It is possible to filter the list of default block categories using the
block_categories
filter. You can do it on the server by implementing a function which returns a list of categories. It is going to be used during blocks registration and to group blocks in the inserter. You can also use the second provided param$post
to generate a different list depending on the post's content.How has this been tested?
Renamed the category of one of the blocks to
my-category
and included the code shared above to activate this category.I also tried what happens when some existing categories get removed. You see warnings (expected non-destructive errors triggered by our code) on the console informing that blocks couldn't be registered because a given category doesn't exist. Which is more than expected.
Screenshots
Types of changes
New feature (non-breaking change which adds functionality)
Checklist: