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

Merged
merged 3 commits into from Jun 28, 2018

Conversation

@gziolo
Member

gziolo commented Jun 28, 2018

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.

add_filter( 'block_categories', function( $categories, $post ) {
	if ( $post->post_type !== 'post' ) {
		return $categories;
	}
	return array_merge(
		$categories,
		array(
			array(
				'slug' => 'my-category',
				'title' => __( 'My category', 'my-plugin' ),
			),
		)
	);
}, 10, 2 );

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

screen shot 2018-06-28 at 15 34 33

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added this to the 3.2 milestone Jun 28, 2018

@gziolo gziolo self-assigned this Jun 28, 2018

@gziolo gziolo added this to To do in Extensibility via automation Jun 28, 2018

@gziolo gziolo requested review from mcsf, mtias and WordPress/gutenberg-core Jun 28, 2018

@gziolo gziolo moved this from To do to In progress in Extensibility Jun 28, 2018

@mcsf

mcsf approved these changes Jun 28, 2018

LGTM!

@aduth

aduth approved these changes Jun 28, 2018

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 ) {

This comment has been minimized.

@aduth

aduth Jun 28, 2018

Member

FYI this example is not PHP 5.2.x compliant (with use of closure, vs. named function).

This comment has been minimized.

@gziolo

gziolo Jun 28, 2018

Member

Yes, phpcs complained when I pasted it into codebase. I will split it into 5.2.x style before merging.

This comment has been minimized.

@gziolo

gziolo Jun 28, 2018

Member

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 ) {

This comment has been minimized.

@aduth

aduth Jun 28, 2018

Member

Should we want to keep the DEFAULT_CATEGORIES in the client?

This comment has been minimized.

@youknowriad

youknowriad Jun 28, 2018

Contributor

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.

This comment has been minimized.

@aduth

aduth Jun 28, 2018

Member

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).

This comment has been minimized.

@gziolo

gziolo Jun 28, 2018

Member

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.

This comment has been minimized.

@youknowriad

youknowriad Jun 28, 2018

Contributor

There's already some previous work done by @aduth bin/get-server-blocks.php that can achieve something close to what we need.

@gziolo gziolo merged commit 2e29bb3 into master Jun 28, 2018

2 checks passed

codecov/project Absolute coverage decreased by -0.22% but relative coverage increased by +2.73% compared to ef5cfe0
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

Extensibility automation moved this from In progress to Done Jun 28, 2018

@gziolo gziolo deleted the add/set-categories-filter branch Jun 28, 2018

@mtias mtias referenced this pull request Jul 4, 2018

Closed

Categories #7713

@ravishakya ravishakya referenced this pull request Aug 31, 2018

Closed

Adding Custom Category #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment