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: Make it possible to override the default class name generation logic #5651

Merged
merged 4 commits into from Mar 18, 2018

Conversation

3 participants
@gziolo
Member

gziolo commented Mar 15, 2018

Description

Implements #5299.

As noted by @mtias in one of the comments (https://github.com/WordPress/gutenberg/pull/5226/files/0dab724e701d9760e8c4c8929a021d0306c17dd5#r170363193), we should make it possible to override the default implementation which is used to generate the class name for the block.

How Has This Been Tested?

Manually using the example from README:

https://github.com/WordPress/gutenberg/blob/ac40937e784ce729e7a2b50ac5dc73cbd3874b83/docs/extensibility.md#blocksgetblockdefaultclassname

// Our filter function
function setBlockCustomClassName( className, blockName ) {
	return blockName === 'core/code' ?
		'my-plugin-code' :
		className;
}

// Adding the filter
wp.hooks.addFilter(
	'blocks.getBlockDefaultClassName',
	'my-plugin/set-block-custom-class-name',
	setBlockCustomClassName
);

With this code applied, the class name generated for code block should be my-plugin-code - this might break your content :)

Checklist:

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

@gziolo gziolo self-assigned this Mar 15, 2018

@gziolo gziolo requested review from mcsf, mtias and aduth Mar 15, 2018

@gziolo gziolo added this to To do in Extensibility via automation Mar 15, 2018

@gziolo gziolo moved this from To do to In progress in Extensibility Mar 15, 2018

@mcsf

mcsf approved these changes Mar 16, 2018

Pending minor bits, looks good.

Show outdated Hide outdated docs/extensibility.md
Show outdated Hide outdated docs/extensibility.md

@gziolo gziolo merged commit 9ebb918 into master Mar 18, 2018

2 checks passed

codecov/project Absolute coverage decreased by -<.01% but relative coverage increased by +16.26% compared to 8434cc9
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

Extensibility automation moved this from In progress to Done Mar 18, 2018

@gziolo gziolo deleted the add/generate-class-name-filter branch Mar 18, 2018

@onetrev

This comment has been minimized.

Show comment
Hide comment
@onetrev

onetrev Jul 9, 2018

Only problem is this doesn't help you for core blocks that don't have classes assigned by default, ie. core/list. Could this be modified to enable developers to add class names to those kinds of blocks or is there another solution for those situations?

onetrev commented Jul 9, 2018

Only problem is this doesn't help you for core blocks that don't have classes assigned by default, ie. core/list. Could this be modified to enable developers to add class names to those kinds of blocks or is there another solution for those situations?

@mcsf

This comment has been minimized.

Show comment
Hide comment
@mcsf

mcsf Jul 9, 2018

Contributor

@onetrev, you could use a filter to add default class names like so:

addFilter( 'blocks.registerBlockType', 'onetrev/class-names/list-block', ( settings, name ) => {
	if ( name !== 'core/list' ) {
		return settings;
	}

	return {
		...settings,
		supports: {
			...settings.supports,
			className: true,
		},
	};
} );
Contributor

mcsf commented Jul 9, 2018

@onetrev, you could use a filter to add default class names like so:

addFilter( 'blocks.registerBlockType', 'onetrev/class-names/list-block', ( settings, name ) => {
	if ( name !== 'core/list' ) {
		return settings;
	}

	return {
		...settings,
		supports: {
			...settings.supports,
			className: true,
		},
	};
} );
@onetrev

This comment has been minimized.

Show comment
Hide comment
@onetrev

onetrev Jul 9, 2018

Ah, interesting. Thanks for that @mcsf ! This will help other folks out too I'm sure as I can see this being a common request. I ended up doing it using blocks.getSaveContent.extraProps filter. Probably not as good way to do it, but it works. ;)

onetrev commented Jul 9, 2018

Ah, interesting. Thanks for that @mcsf ! This will help other folks out too I'm sure as I can see this being a common request. I ended up doing it using blocks.getSaveContent.extraProps filter. Probably not as good way to do it, but it works. ;)

@mcsf mcsf referenced this pull request Jul 20, 2018

Merged

Docs: Add example for registerBlockType hook #8091

0 of 4 tasks complete
@mcsf

This comment has been minimized.

Show comment
Hide comment
@mcsf

mcsf Jul 20, 2018

Contributor

Opened #8091 to add the above example to the docs

Contributor

mcsf commented Jul 20, 2018

Opened #8091 to add the above example to the docs

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