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

Proper way to use Objects in Block Attributes #27757

Closed
HardeepAsrani opened this issue Dec 16, 2020 · 1 comment
Closed

Proper way to use Objects in Block Attributes #27757

HardeepAsrani opened this issue Dec 16, 2020 · 1 comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@HardeepAsrani
Copy link
Contributor

Hey,

I searched about this quite a bit and couldn't find any leads so the final resource was to ask here. What's the proper way of using objects in attributes? Currently I'm using like this:

hide: {
	type: 'object',
	default: {
		global: false,
		tablet: false,
		mobile: false
	}
},

But is there a more structured way with array attributes, similar to how REST API handles objects https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#objects?

Sorry for bugging here.

@carolinan carolinan added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Dec 19, 2020
@johngodley
Copy link
Contributor

johngodley commented Aug 4, 2021

It's maybe a bit late now, but I've started a PR to expand the block attributes documentation:

#33880

With regard to your question you could provide more structure to the object definition by using a query source (there is an example in the block attributes query section).

It's not possible to add type information if the object is saved to the default block comment (as above in your example), and you will need to verify the object yourself.

The alternative is that you split the object out into separate attributes (this is what core blocks mostly do), with each attribute specifying the type. That is, in your example you would have 3 attributes - global, tablet, and mobile - each of which is a boolean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

3 participants