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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { select } from '@wordpress/data'; | ||
import { dispatch, select } from '@wordpress/data'; | ||
|
||
/** | ||
* Returns all the block categories. | ||
* | ||
* @return {Array} Block categories. | ||
* @return {Object[]} Block categories. | ||
*/ | ||
export function getCategories() { | ||
return select( 'core/blocks' ).getCategories(); | ||
} | ||
|
||
/** | ||
* Sets the block categories. | ||
* | ||
* @param {Object[]} categories Block categories. | ||
*/ | ||
export function setCategories( categories ) { | ||
dispatch( 'core/blocks' ).setCategories( categories ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,9 +189,30 @@ On the server, you can filter the list of blocks shown in the inserter using the | |
|
||
```php | ||
add_filter( 'allowed_block_types', function( $allowed_block_types, $post ) { | ||
if ( $post->post_type === 'post' ) { | ||
if ( $post->post_type !== 'post' ) { | ||
return $allowed_block_types; | ||
} | ||
return [ 'core/paragraph' ]; | ||
return array( 'core/paragraph' ); | ||
}, 10, 2 ); | ||
``` | ||
|
||
## Managing block categories | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yes, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in e75c4f3. |
||
if ( $post->post_type !== 'post' ) { | ||
return $categories; | ||
} | ||
return array_merge( | ||
$categories, | ||
array( | ||
array( | ||
'slug' => 'my-category', | ||
'title' => __( 'My category', 'my-plugin' ), | ||
), | ||
) | ||
); | ||
}, 10, 2 ); | ||
``` |
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.