Skip to content
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

addAttribute() helper function overwrites blocktype attributes settings #15240

Closed
elliotcondon opened this issue Apr 29, 2019 · 4 comments · Fixed by #15959
Closed

addAttribute() helper function overwrites blocktype attributes settings #15240

elliotcondon opened this issue Apr 29, 2019 · 4 comments · Fixed by #15959
Assignees
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Type] Bug An existing feature does not function as intended

Comments

@elliotcondon
Copy link

elliotcondon commented Apr 29, 2019

Describe the bug
When registering a block type that supports "anchor", the attributes.anchor setting is being overwritten by a helper function addAttribute().

This helper function is designed to work with a static block type that returns an element from the save() function but is causing issues with dynamic block types that return null.

As an example, here is a dynamic block type that allows "anchor" and expects the anchor value to be serialized within the block HTML JSON.

// Register with WP.
var result = wp.blocks.registerBlockType( 'test/my-block', {
	title: 'Test block',
	attributes: {
		anchor: {
			type: 'string'
		}
	}
	supports: {
		anchor: true
	}
	save: function(){
		return null;
	}
});

Even though the anchor attribute has been defined for the block type, the result will actually look like this because the attributes.anchor setting has been overwritten by the addAttribute() helper function.

{
	title: 'Test block',
	attributes: {
		anchor: {
			type: 'string',
			source: 'attribute',
			attribute: 'id',
			selector: '*',
		}
	}
	supports: {
		anchor: true
	}
	save: function(){
		return null;
	}
}

Expected behavior
If the attributes.anchor setting has already been specified when registering a block type, it should not be overwritten by the helper function.

Additional info
You can use the following line of code to remove the helper function, which allows dynamic block type to save "anchor" data, but obviously causes issues with core, and is not a valid solution:

wp.hooks.removeFilter( 'blocks.registerBlockType', 'core/anchor/attribute' );
@elliotcondon elliotcondon changed the title addAttribute helper function overpowers blocktype settings addAttribute() helper function overwrites blocktype attributes settings Apr 29, 2019
@sarahquigley
Copy link

+1 on this. Causing big issues on our projects.

@okadots
Copy link

okadots commented Apr 29, 2019

+1 definitely need to resolve this

@swissspidy swissspidy added [Feature] Block API API that allows to express the block paradigm. Needs Technical Feedback Needs testing from a developer perspective. labels May 15, 2019
@gziolo gziolo added [Type] Bug An existing feature does not function as intended [Feature] Extensibility The ability to extend blocks or the editing experience Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts and removed Needs Technical Feedback Needs testing from a developer perspective. labels May 24, 2019
@gziolo
Copy link
Member

gziolo commented May 24, 2019

It looks like something that needs to be fixed. We can follow what already exists for supports: align:

https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/hooks/align.js#L78-L81

@gziolo
Copy link
Member

gziolo commented Jun 4, 2019

It should be fixed with #15959.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants